ASP .NET 批量文件上传

  • A+
所属分类:.NET技术
摘要

1.前台View2.后台Controller 

1.前台View

ASP .NET  批量文件上传ASP .NET  批量文件上传

<body>     <div>         <table>             <tr>                 <td>                     <input type="file" id="files" name="files" multiple="multiple">                     <input type="submit" id="up" onclick="up()" value="上传" />                     <div id="content"></div>                 </td>             </tr>             <tr>                 <td><a href="UploadGet_KPI2Zip">下载</a></td>             </tr>         </table>      </div> </body>

JS

ASP .NET  批量文件上传ASP .NET  批量文件上传

<script>     var inputElement = document.getElementById("files");     inputElement.addEventListener("change", handleFiles, false);      function handleFiles() {         var fileList = this.files;         var dd = document.getElementById('content');         for (var i = 0 ; i < fileList.length ; i++) {             dd.innerHTML += fileList[i].name + "<br>";         }     }     function up() {         var fileObj = document.getElementById("files").files;         var params = new FormData();         for (var i = 0; i < fileObj.length; i++) {             params.append("file" + i, fileObj[i]);         }         $.ajax({             type: "POST",             url: "UploadFiles",             data: params,             contentType: false,//必须false才会自动加上正确的Content-Type              processData: false,//必须false才会避开jQuery对 formdata 的默认处理 ,XMLHttpRequest会对 formdata 进行正确的处理               success: function (result) {                 alert(result.msg);             },             error: function () {                 alert("Error");             }         });         return false;     }  </script>

script

2.后台Controller

ASP .NET  批量文件上传ASP .NET  批量文件上传

  [HttpPost]          public ActionResult UploadFiles(HttpPostedFileBase f)         {             if (!Request.IsAjaxRequest())                 return View();             try             {                 var files = Request.Files;                 if (files.Count == 0)                     return Json(GetResult(false, "提示:没有要上传的文件!", null));                  var i = 1;                 foreach (string file in files)                 {                     //获取文件信息                     var curFile = Request.Files;                     if (curFile != null && curFile.ContentLength < 1)                         continue;                     //获取保存路径                     var filesUrl = Server.MapPath("~/Upload");                     if (Directory.Exists(filesUrl) == false)//路径不存在则创建                         Directory.CreateDirectory(filesUrl);                     if (curFile == null)                         continue;                     //获取文件名                     var fileName = Path.GetFileName(curFile.FileName);                     if (fileName == null)                         continue;                     //文件后缀名                     var filePostfixName = fileName.Substring(fileName.LastIndexOf('.'));                     //新文件名                     var newFileName = (DateTime.Now.ToString("yyyyMMddHHmmss") + i) + filePostfixName;                     i++;                     //                     var path = Path.Combine(filesUrl, newFileName);                     //保存文件                     curFile.SaveAs(path);                 }                 return Json(GetResult(true, "上传成功!", null));             }             catch (Exception ex)             {                 return Json(GetResult(false, "上传失败、错误信息:" + ex.Message, null));             }         }

Controller