Can I use:
<input type="file" name="files" id="files" multiple="multiple" />
and bind it to:
[HttpPost]
public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
{
...
}
I'm writing a web app for modern browsers and don't have to worry about IE so I'd like to avoid using Flash. Right now files
is always null when I post the form. Is there any way to get this to work in MVC 3?
Thanks!
Do you have your encoding set correctly in your form?
I believe you still need:
new { enctype = "multipart/form-data" }
In the form declaration to ensure the browser can post files.
For example:
@using (Html.BeginForm("action", "controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
Wouldn't one use the Request.Files for backward compatibility as follows:
public ActionResult UploadFiles()
{
string UpoadedFilesFolder = "YourServerFolder";
string fileName ="";
byte[] fileData=null;
foreach (HttpPostedFileBase uf in Request.Files)
{
HttpPostedFileBase UpoadedFile = uf;
if (uf.ContentLength > 0)
{
fileName = Path.GetFileName(UpoadedFile.FileName);
using (BinaryReader br = new BinaryReader(UpoadedFile.InputStream))
{
fileData = br.ReadBytes((int)UpoadedFile.InputStream.Length);
}
using (FileStream fs = new FileStream(Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath(UpoadedFilesFolder), fi.FileName), FileMode.Create))
{
fs.Write(fileData, 0, fileData.Length);
}
}
}
return Content("OK");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With