Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
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.
[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
} |