I am trying to upload multiple images using ASP.NET MVC and Ajax. Was able to get the code to work and upload 1 image but finding it difficult to upload multiple images in a separate image folder. Appreciate any help.
please find the HTML code
<div class="row" style="margin-top:20px;"> <div class="col-md-6 col-sm-6 col-xs-12"> <div class="col-md-4" style="margin:0 !important;"><label style="margin-top:5px; margin-left: -15px;">Select image</label></div> <div class="col-md-8" style="margin:0 !important;"> <span class="control-fileupload "> <input type="file" id="Fimage0" name="ImageUpload" onchange='uploadImage(0)' class="form-control clearMembers"> </span> </div> </div> <div class="col-md-6 col-sm-6 col-xs-12"> <div class="col-md-4" style="margin:0 !important;"><label style="margin-top:5px; margin-left: -15px;">Select image (Spouse)</label></div> <div class="col-md-8" style="margin:0 !important;"> <span class="control-fileupload "> <input type="file" id="Fimage1" name="ImageUpload" onchange='uploadImage(1)' class="form-control clearMembers"> </span> </div> </div> </div> please find the script,
as i have done that get all value in array but i unable to pass the value to ajax append please fine the below ajax.
var file; var imagearray = []; function uploadImage(Imageid) { debugger var fileUpload = document.getElementById("Fimage" + Imageid); file = fileUpload.files[i]; imagearray.push(file) } please find the ajax
function SaveFamilyInfoDatatoDB() { var formData = new FormData(); formData.append("Name", $('#FName').val()); //formData.append("file", $('#Fimage')[0].files[0]); //formData.append("file", $('#FimageSpouse')[0].files[0]); formData.append("file", $('#Fimage0')[0].files[0]); $.ajax({ type: "POST", url: "@Url.Action("SaveAndUpdateFamilyInfo","FamilyInfo")", datatype: "JSON", processData: false, contentType: false, data: formData, processData: false, contentType: false, success: function (Result) { if (Result.type == "success") { pushToDocumentArray(); } else if (Result.type == "NicValidation") { alert("NIC Already Added") } else { alert("11") } } }) } please find the controller
public JsonResult SaveAndUpdateFamilyInfo(Family_Information FamilyInfoMainDeatils, HttpPostedFileBase[] file) { try { string imgepath = "Null"; if (file != null) { for (int i = 0; i < file.Length; i++) { } //string filename = file.FileName; //imgepath = filename; //string extension = Path.GetExtension(file.FileName); //// filename = filename + DateTime.Now.ToString("yymmssfff") + extension; //// person.ImagePath = "~/Ima/" + filename; //var path = Path.Combine(Server.MapPath("~/Images"), FamilyInfoMainDeatils.Name + filename); //file.SaveAs(path); } string FamilyInfoID = Adapter.SaveAndUpdateFamilyInfo_(FamilyInfoMainDeatils, imgepath); return Json(new { type = FamilyInfoID }); } catch (Exception ex) { Log.ErrorLog(ex.Message); throw; } }
HttpPostedFileBase[] ImageUpload(to match the name of the input), and simply usevar formdata = new FormData($('form').get(0));(and delete youruploadImage()function and theonchangein your inputs.var formData = new FormData();and not getting that error, so what you claiming is not possible (but it should not be wrapped in parethesis)formData.appendlines of code - thevar formdata = new FormData($('form').get(0));already add all the values of the form controls as explained in the link I gave you (assuming you generate your view correctly which you have not - ALWAYS ALWAYS generate you view with the@Html.***For(m => m.yourProperty)methods.