Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4258410
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:38:44+00:00 2026-05-21T05:38:44+00:00

I have a problem with uploading file in asp.net mvc 2. My controller function’s

  • 0

I have a problem with uploading file in asp.net mvc 2.
My controller function’s parameter is a FormCollection type. Because the fields are too numerous, I can’t separate each field as a parameter. I have 2 upload file fields in my form. How can I get uploaded files in my controller?

I tried this way:

public ActionResult CreateAgent(FormCollection collection, HttpPostedFileBase personImage)
{
    ...
}

but personImage was null. 🙁

or this way:

HttpPostedFileBase img = this.HttpContext.Request.Files[collection["personImage"]];

but img was null to. Also collection["personImage"] was the name of selected file (without path) and I can’t cast it to HttpPostedFileBase.

Note that all fields must be filled in on one page. I can’t let the customer upload images in a separate page!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:38:44+00:00Added an answer on May 21, 2026 at 5:38 am

    Start by reading this blog post. Then apply it to your scenario:

    <form action="/Home/CreateAgent" method="post" enctype="multipart/form-data">
    
        <input type="file" name="file1" id="file" />
        <input type="file" name="file2" id="file" />
    
        ... Some other input fields for which we don't care at the moment
            and for which you definetely should create a view model
            instead of using FormCollection in your controller action
    
        <input type="submit" />
    </form>
    

    which translated in WebForms language gives:

    <% using (Html.BeginForm("CreateAgent", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { %>
        <input type="file" name="file1" id="file" />
        <input type="file" name="file2" id="file" />
    
        ... Some other input fields for which we don't care at the moment
            and for which you definetely should create a view model
            instead of using FormCollection in your controller action
    
        <input type="submit" />
    <% } %>
    

    and then:

    public ActionResult CreateAgent(
        // TODO: To be replaced by a strongly typed view model as the 
        // ugliness of FormCollection is indescribable
        FormCollection collection, 
        HttpPostedFileBase file1, 
        HttpPostedFileBase file2
    )
    {
        // use file1 and file2 here which are the names of the corresponding
        // form input fields
    }
    

    If you have many files then use IEnumerable<HttpPostedFileBase> as illustrated by Haacked.

    Remarks:

    • Absolutely never use this.HttpContext.Request.Files in an ASP.NET MVC application
    • Absolutely never never never use this.HttpContext.Request.Files[collection["personImage"]] in an ASP.NET MVC application.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a problem with uploading. if ((($_FILES[file][type] == image/gif) || ($_FILES[file][type] == image/jpeg)
I have problem in uploading .doc file to .Net WCF from my Android app.
all. I have a problem with uploading of the file in Silverlight application. Here
I have an ASP.NET MVC web app which includes the facility for clients to
I am uploading files using HttpWebRequest to an ASP.Net MVC application but, for some
I have a problem uploading file to a network shared folder. I can connect
I have some weird problem while uploading a file with a Cyrillic name using
I have a problem with my webapplication. I have made an upload-function for uploading
I have a JavaScript function below where it displays a message after file uploading
I have a problem conserning uploading multiple files to my ftp server and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.