<input id="FileUpload" multiple type="file" onchange="funChange()"/><br /> <ul id="ulList"> </ul> <input id="btnShow" type="button" onclick="saveFiles()" value="保存" />
<script type="text/javascript"> function funChange() { var files = $("#FileUpload")[0].files; $("#FileUpload").data("jxp", files); }; function saveFiles() { $("#ulList").empty(); var fragment = ""; var formData = new FormData(); var cacseFiles = $("#FileUpload").data("jxp"); if (!cacseFiles) { alert("$.cache缓存中不存在上传数据!"); return; } var length = cacseFiles.length; for (var i = 0; i < length; i++) { var file = cacseFiles[i]; formData.append("FileUpload", file); var fileName = file.name; var fileSize = file.size; var fileType = file.type; fragment += "<li>文件名:" + fileName + " (文件大小:<b>" + fileSize + "</b> bytes) 文件类型 :" + fileType + "</li>"; } $("#ulList").html(fragment); $.ajax({ url: '<%=Url.Content("~/Home/GetFiles") %>', type: 'POST', processData: false, contentType: false, data: formData, success: function (data) { if (data.IsSuccessed) { alert(data.msg); } } }); } </script>
public ActionResult GetFiles(List<HttpPostedFileBase> FileUpload) { if (FileUpload == null || FileUpload.Count <= 0) { return Json(new { IsSuccessed = false, msg = "未上传文件!" }); } foreach (var file in FileUpload) { var fileName = Path.GetFileName(file.FileName); var dic = Server.MapPath("~/Junk/"); if (!Directory.Exists(dic)) { Directory.CreateDirectory(dic); } if (fileName == null) continue; var path = Path.Combine(dic, fileName); file.SaveAs(path); } return Json(new { IsSuccessed = true, msg = "上传成功。" }); }