For the file upload control on the View, the post action will return a HttpPostedFileBase class object to the Controller. HttpPostedFileBase serves as the base class for classes that provide access to individual files that have been uploaded by a client.
The view for File upload and Submit button on it will be as below:
@using (Html.BeginForm("OpenFile", "Home", FormMethod.Post,
<input id="fileControl" type="file" name="Document" />
<input type="submit" value="Open File" />
is added because when you make a POST request, you have to encode the data that forms the body of the request in some way. HTML forms provide two methods of encoding. The default is
application/x-www-form-urlencoded, which is more or less the same as a query string on the end of the URL.
multipart/form-data is a more complicated encoding but one which allows entire files to be included in the data. Are far as the client is concerned: Use
multipart/form-data when you have an
Now the Home controller action OpenFile will be like,
public FileContentResult OpenFile(HttpPostedFileBase Document)
byte data = null;
if (Document != null)
using (Stream inputStream = Document.InputStream)
MemoryStream memoryStream = inputStream as MemoryStream;
if (memoryStream == null)
memoryStream = new MemoryStream();
data = memoryStream.ToArray();
return File(data, Document.ContentType, "SampleDoc.pdf");
Default Content-Disposition is attachment in IE. This will prompt for the Save dialog but inorder to open the file on the browser, add Content-Disposition as inline to the Response object before return File()...
Response.AddHeader("Content-Disposition", "inline; filename=test.pdf");
Find the complete working solution at - FileUploadDemo.zip