jquery上传文件
function initFileUpload(id) { var file = $("#" + id + ""); var files = file[0].files; var formData = new FormData(); for (var i = 0; i < files.length; i++) { formData.append(files[i].name, files[i]); } //formData.append("name", "刘大大");//可追加参数 $.ajax({ url: "@Url.Action("UploadImage")", type: "POST", async: true, dataType: "FromData", data: formData, contentType: false, processData: false, success: function (data) { console.log("data:"); console.log(data); } }); }
后台:
public ActionResult UploadImage() { var files = HttpContext.Request.Form.Files; if (files.Count > 0) { var fileDirectory = Path.Combine(_hostingEnvironment.ContentRootPath, Config.UploadVirtualPath, "SupplierImage"); } return Json(new { success = true }); }
html
<input title="img" type="file" onchange="initFileUpload(\''+data[j].ProcessFlowFieldName+'\')" name='+data[j].ProcessFlowFieldName+' id=\''+data[j].ProcessFlowFieldName+'\' accept=".png,.jpg,jpeg" onchange="$(\'#location\').val($(\'#'+data[j].ProcessFlowFieldName+'\').val());" style="display: none">