


The value providers are the components that gets the value needed from the particular source (query-strings, form etc.)Īnd feed to binders. The model binding feature relies on two types of components binders and value providers. In some cases we need to convert HttpPostedFileBase to HttpPostedFile and we can achieve that using the HttpPostedFileWrapper.Īs many of us already aware, it's the model binding feature that maps the POSTed file to HttpPostedFileBase in the action parameter.īut what we are interested here is to know the supporting classes. So wherever you need to read files, I advise you to use HttpPostedFileBase instead of HttpPostedFile.Īn important thing to note down is we can't directly substitute HttpPostedFile with HttpPostedFileBase because HttpPostedFile is still the same, not derives from any type. The HttpPostedFileBase is created to substitute HttpPostedFile in MVC applications for better unit testing.
#Asp.net file upload example code#
There was only HttpPostedFile which is concrete and sealed making the code hard to unit-test. HttpPostedFileBase is an abstract class that contains the same members as in HttpPostedFile. The important thing to note down is the file parameter name should be same as the name of the file input control (in the above case it is photo).

Using HttpPostedFileBase as action parameter String directory = != null & photo.ContentLength > 0) Public ActionResult Upload(HttpPostedFileBase photo) Instead of manually reading the file from the Request, by taking the advantage of model binding the file can be made directly available as a parameter in the action as shown in the below listing. Photo.SaveAs(Path.Combine(directory, fileName))

Var fileName = Path.GetFileName(photo.FileName) If(photo != null & photo.ContentLength > 0) String directory = photo = Request.Files
#Asp.net file upload example how to#
In the below listing we can see how to read the POSTed file from the request and save to the server. The POSTed files are available in HttpFileCollectionBase of Request.Files. If you forget setting the proper encoding type then only the When you post the form the Content-Type header is set to multipart/form-data. As default the encoding type isĪpplication/x-www-form-urlencoded which is insufficient for sending large amount of data to server especially when the form contains files, non-ASCII data All we need is a html form having encoding type set to multipart/form-data and a file input control.Īn important thing developers forget sometimes is setting the encoding type(enctype) to multipart/form-data. POSTing a file to the server is quite simple. Thanks for all the readers who pointed out the errors and typos in the article. I'm sure this article will help the MVC programmers to increase their grip on the framework. In different posts, threads in a single place. So why another article? Well, in this article I gathered the important concepts that are scattered There are already plenty of articles written on this subject. The files in the server can be easily sent as response to the clients through its rich support of action results. The POSTed file(s) are available as parameters directly in actions through model binding. Uploading and returning files in an ASP.NET MVC application is very simple. Controller helper methods to return files How a browser knows what file type is returned from the server? Using view models to validate POSTed files
