I have a requirement of uploading the multiple documents with the help of file upload control. The same thing I had done for the Image type. It is working perfectly since it takes only 1 image file. But the multiple file upload is not working. What's the flaw in my code How can I do it?
Html Code:
----------
@using (Html.BeginForm("Index", "Block", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="col-lg-4" id="FileUp">
<label>Upload File</label>
@Html.TextBoxFor(m=>m.DocumentFilesPath, new { @name="fileUpload", @id="fileUpload", @type = "file", @multiple="multiple", @class="form-control dropDownLabel", @onchange="fileTypeCheck()" } )
</div>
// What should I specify the type for DocumentFilesPath in @Html.TextBoxFor(m=>m.DocumentFilesPath) in my strongly typed model?
<div class="col-lg-8>
<button type="button" class="btn btn-primary" id="SaveBlock" onclick="checkSubmit()">
<span class="glyphicon glyphicon-floppy-save"></span>Save
</button>
</div>
}
My Controller Action:
---------------------
[Authorize]
[HttpPost]
public ActionResult Index(BlockViewModel model)
{
if (model.BlockID == 0)
{
HttpPostedFileBase file = Request.Files["imgUpload"] as HttpPostedFileBase; // I am able to get the single file here
if (file.ContentLength > 0)
{
file.SaveAs(Server.MapPath("~/Images/uploads/") + file.FileName);
model.ImageFilepath = Server.MapPath("~/Images/uploads/") + file.FileName;
}
IEnumerable<HttpPostedFileBase> docFiles = Request.Files["fileUpload"] as IEnumerable<HttpPostedFileBase>; // But here docFiles is null
//IEnumerable<HttpPostedFileBase> docFiles = fileUpload as IEnumerable<HttpPostedFileBase>;
if (docFiles != null)
{
foreach (HttpPostedFileBase docFile in docFiles)
{
if (docFile.ContentLength > 0)
{
docFile.SaveAs(Server.MapPath("~/Images/Files/") + docFile.FileName);
model.DocumentFilesPath = Server.MapPath("~/Images/Files/") + docFile.FileName;
}
}
}
}
Request.Files["imgUpload"]? Just add a parameterIEnumerable<HttpPostedFileBase> documentFilesPathto the method (and remove the pointlessnew { @name="fileUpload" }in your view which does nothing)fileTypeCheck()?multiple="multiple"attribute (as OP is doing)