Uploading file in ASP.NET MVC is not as straight job as in ASP.NET as you have to do the ASP.NET FileUpload server control job yourself. You have to follow following simple steps.

1.Set enctype property of the form to "Multipart/form-data" as shown below. By default enctype of form is “application/x-www-form-urlencoded"

The page contains an input field of type “file” and button to submit the form.

By adding setting enctype to "Multipart/form-data", HTTP takes cares of the files uploaded before submission.

2. Add logic to grab the file in the controller that is being used to update the form say it’s “Edit”. When the form having enctype = "Multipart/form-data" is submitted the Request object will contain a collection of files, iterate this collection to get the desired file.

That’s it.

Just for your info you might have seen that the built in HttpPostedFileBase class plays key role in getting correct file’s content type and saving it. This class has following members that almost all we have used in this example!

Hope it helps.

1 comments:

Thanks for the code. Does that implicit variable work for you? For some reason I can't access the length attribute unless I declare a variable of type byte[]...Nonetheless, this got me 99.9% of the way.