即使我上传多个文件,MVC也会接收一个文件

时间:2018-11-05 02:43:26

标签: jquery ajax

我正在使用Bootstrap File Input-Kartik插件在MVC 5应用程序中上传多个文件。 我创建了以下HTML标记

<input id="fileUpload" type="file" class="file" multiple>

然后我像这样初始化文件上传器

$fileTC.fileinput({
        uploadUrl: "/TacPham/Upload",
        uploadAsync: false,
        browseOnZoneClick: true,
        allowedFileTypes: ['image']
    });

我正在尝试使用Ajax请求将文件上传到服务器。

$('#btnXuLy').click(function () {
        var model = {
            tenTacPham: $('#txtTenTacPham').val(),
            loaiTacPham: $('#seLoaiTacPham').val(),
            chuDe: $('#seChuDe').val(),
            noiDung: $('#taNoiDung').val(),
            idNguoiDung: hid_IDNguoiDung_Default,
            email: hid_Email_Default
        };

        $fileTC.fileinput('refresh', {
            uploadExtraData: model
        });

        $fileTC.fileinput('upload');

        $fileTC.on('filebatchuploadsuccess', function (event, data) {
            var form = data.form, files = data.files, extra = data.extra, response = data.response;

            console.log(data);
            console.log(response);
            console.log(files);
            console.log(extra);
            console.log('File batch upload success');
        });
})

点击“上传”按钮后,请求已按预期到达我的服务器,但我的服务器只会看到一个文件,而不是全部文件。

这是我的方法的样子

[HttpPost]
        public ActionResult Upload(TacPhamJsonModel model)
        {
            var error = "";
            foreach (string item in Request.Files)
            {
                  HttpPostedFileBase getEle = Request.Files[item] as HttpPostedFileBase;
                  var uploadPath = "~/Documents/";
                  var finalPath = Path.Combine(HttpContext.Request.MapPath(uploadPath), getEle.FileName);
                  getEle.SaveAs(finalPath);
             }
             return Json(error);
        }

即使我同时上传了3或5个文件,Request.Files.Count也返回1。

0 个答案:

没有答案