Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 17. Model Binding > Using Model Binding to Receive File Uploads

17.4. Using Model Binding to Receive File Uploads

All we have to do to receive uploaded files is to define an action method that takes a parameter of the HttpPostedFileBase type. The model binder will populate it with the data corresponding to an uploaded file. Listing 17-23 shows an action method that receives an uploaded file.

Example 17.23. Receiving an Uploaded File in an Action Method

[HttpPost]
public ActionResult Upload(HttpPostedFileBase file) {

    // Save the file to disk on the server
    string filename = "myfileName"; // ... pick a filename
    file.SaveAs(filename);

// ... or work with the data directly
    byte[] uploadedBytes = new byte[file.ContentLength];
    file.InputStream.Read(uploadedBytes, 0, file.ContentLength);
    // Now do something with uploadedBytes
}


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial