MVC多文件上传

多文件传输 

HTML: 

 

js:

        var count = 1; //上传组件个数
        $(function () {
            //添加上传组件
            $("#btnAdd").click(function () {
                if ($("#DivUploads").find(":button").length >= 8) {
                    alert('最多只能添加八个上传组件!');
                    return;
                }
                var strHtml = '';
                strHtml += "";
                $("#DivUploads").append(strHtml);
                count++;
            });
        });
        //删除上传组件
        function delUploadBtn(index) {
            $("#DivUploads").find(":button").each(function () {
                var text = "" + $(this).attr("onclick");
                if (text.indexOf("delUploadBtn(" + index + ")") != -1) {
                    $(this).parent().remove();
                }
            });
        }

控制器方法:

       public ActionResult Upload(HttpPostedFileBase[] files)
       {
            var path = Server.MapPath("/File");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            foreach (HttpPostedFileBase item in files)
            {
                if (item != null)
                {
                    item.SaveAs(Path.Combine(path, item.FileName));
                }
            }
            return null;
        }

 

你可能感兴趣的:(MVC,C#)