天天看点

.net c# 实现多文件上传

公司业务需求 在上传文件功能处实现按住Ctrl实现多选  不多BB直接上代码

HTML

   选择图片:<input type="file" id="descFile" name="descFile" οnchange="fileUpload()" multiple="multiple" /> 

JS

 function fileUpload() {

        var files = document.getElementById("descFile").files; 

        for (var i = 0; i < files.length; i++) {

            var file = document.getElementById("descFile").files[i];

            var fileName = file.name; 

            var index = fileName.lastIndexOf('.');

            var newName = fileName.substring(0, index);

            var fd = new FormData();

            fd.append('username', 'root')

            fd.append('myfile', file);

            $.ajax({

                url: '/Home/Upload/?source=' + $("#s_CaseSerial").val() + '&&inOut=OutFile',//地址

                type: 'POST',

                data: fd,

                processData: false,  //tell jQuery not to process the data

                contentType: false,  //tell jQuery not to set contentType

                success: function (arg, a1, a2) {

                    $.modalMsg("操作已完成", "warning");

                }

            })

        }

    }

Controller

  public ActionResult Upload(string source = "", string inOut = "")

        {

            HttpPostedFileBase file = Request.Files.Get("myfile");

            if (file.ContentLength == 0)

            {

                return Json(new

                {

                    bRet = false,

                    sMsg = "请选择文件!",

                    data = ""

                }, "text/html");

            }

            if (file.ContentLength > 52428800)

            {

                return Json(new

                {

                    bRet = false,

                    sMsg = "文件大小不能超过50M!",

                    path = ""

                }, "text/html");

            }

            //上传文件代码 记得先新建一下 Upload 文件夹

            //var fileName = Path.Combine(Request.MapPath("~/UploadFile"), Path.GetFileName(file.FileName));

            //string s_NewFileName = arr_FileName[0] + DateTime.Now.ToString("yyyyMMddHHmmss") + "." + arr_FileName[1];

            //string s_FilePath = "Documents\\ClientFile\\" + DateTime.Now.Year + "-" + DateTime.Now.Month + "\\" + s_NewFileName;

            string path = "";

            string fileName = "";

            string returnpath = "";

            string extension = Path.GetExtension(file.FileName);

            string fname = Path.GetFileNameWithoutExtension(file.FileName);

            string newFileName = fname + DateTime.Now.ToString("yyyyMMddHHmmss") + extension;

            if (source == "")

            {

                path = ConfigurationManager.AppSettings["filepath"] + "Documents\\ClientFile\\" + DateTime.Now.Year + "-" + DateTime.Now.Month;

                fileName = path + "\\" + newFileName;

                returnpath = "Documents\\ClientFile\\" + DateTime.Now.Year + "-" + DateTime.Now.Month + "\\" + newFileName;

            }

            else

            {

                path = ConfigurationManager.AppSettings["filepath"] + "Documents\\" + DateTime.Now.Year + "-" + DateTime.Now.Month + "\\" + source.Replace('?', '-') + "\\" + inOut;

                fileName = path + "\\" + newFileName;

                returnpath = "Documents\\" + DateTime.Now.Year + "-" + DateTime.Now.Month + "\\" + source.Replace('?', '-') + "\\"  + inOut + "\\" + newFileName;

            }

            Directory.CreateDirectory(path);

            try

            {

                file.SaveAs(fileName);

                var data = new

                {

                    bRet = true,

                    sMsg = "上传成功",

                    path = returnpath

                };

                return Content(data.ToJson());

            }

            catch

            {

                 var data = new

                {

                    bRet = true,

                    sMsg = "上传失败",

                    path = returnpath

                };

                 return Content(data.ToJson());

            }

        }

//可以用这种方式获取上传的文件名字

   var files = document.getElementById("descFile").files;

        var fileName = "";

        for (var i = 0; i < files.length; i++) {

            var file = document.getElementById("descFile").files[i];

            alert(file.name)

        }