【问题标题】:Error code 400 when uploading files using bootstrap fileinput plugin使用引导文件输入插件上传文件时出现错误代码 400
【发布时间】:2020-12-22 16:58:48
【问题描述】:

我有一个问题,无论我做什么,无论有多少文件,无论我是否删除了 extraData 选项,无论我是否在控制器操作中使用参数,操作都永远不会到达(我的断点永远不会触发)。

HTML 代码:

<div id="logoDiv">
     <input id="fileInput" name="files" type="file" 
      data-allowed-file-extensions='["png", "jpg", "jpeg", "tiff"]' multiple/>
</div>

Javascript 代码:

var plugin = $('#fileInput').fileinput({
        showCaption: false,
        showPreview: true,
        dropZoneEnabled: false,
        theme: "fa",
        browseClass: "btn btn-ey-upload btn-ey-utility",
        removeClass: "btn btn-ey-upload btn-ey-utility",
        uploadClass: "btn btn-ey-upload btn-ey-utility",
        showClose: false,
        uploadAsync: false,
        uploadUrl: "/GeneralInterview/UploadLogo",
        //uploadExtraData: { id: 1 }
    });

控制器代码:

[HttpPost]
public JsonResult UploadFile(List<IFormFile> files)
{
     List<string> fileNames = new List<string>();

     return Json(fileNames)
}

我也尝试过 IActionResult 和 JsonResult。

我真的不知道出了什么问题,任何其他资源似乎都在做我所做的事情,但它只是不起作用。

编辑: 响应和请求标头

content-length: 0
content-security-policy: default-src 'self'; style-src 'self' fonts.googleapis.com 'unsafe-inline'; font-src 'self' fonts.gstatic.com; script-src 'self' https://ajax.aspnetcdn.com 'nonces-SNIP'; img-src 'self' data:;
date: Fri, 04 Sep 2020 07:26:48 GMT
status: 400
x-content-type-options: nosniff
x-frame-options: DENY
x-powered-by: ASP.NET
x-rate-limit-limit: 7d
x-rate-limit-remaining: 999965
x-rate-limit-reset: 2020-09-11T07:24:01.3268956Z
x-sourcefiles: =?UTF-8?B?QzpcVXNlcnNcR1gyNjRMSFxTb3VyY2VcUmVwb3NcRU9ZLUVZXEVPWVdlYlxHZW5lcmFsSW50ZXJ2aWV3XFVwbG9hZExvZ28=?=
x-xss-protection: 1;mode=block


Request URL: https://localhost:44357/GeneralInterview/UploadLogo
Request Method: POST
Status Code: 400 
Remote Address: [::1]:44357
Referrer Policy: no-referrer-when-downgrade
content-length: 0
content-security-policy: default-src 'self'; style-src 'self' fonts.googleapis.com 'unsafe-inline'; font-src 'self' fonts.gstatic.com; script-src 'self' https://ajax.aspnetcdn.com 'nonces-SNIP'; img-src 'self' data:;
date: Fri, 04 Sep 2020 07:26:48 GMT
status: 400
x-content-type-options: nosniff
x-frame-options: DENY
x-powered-by: ASP.NET
x-rate-limit-limit: 7d
x-rate-limit-remaining: 999965
x-rate-limit-reset: 2020-09-11T07:24:01.3268956Z
x-sourcefiles: =?UTF-8?B?QzpcVXNlcnNcR1gyNjRMSFxTb3VyY2VcUmVwb3NcRU9ZLUVZXEVPWVdlYlxHZW5lcmFsSW50ZXJ2aWV3XFVwbG9hZExvZ28=?=
x-xss-protection: 1;mode=block
:authority: localhost:44357
:method: POST
:path: /GeneralInterview/UploadLogo
:scheme: https
accept: application/json, text/javascript, */*; q=0.01
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
content-length: 58916
content-type: multipart/form-data; boundary=----WebKitFormBoundaryDm8BfGYWiNAUmPUI
cookie: .AspNetCore.Antiforgery.cookiedata; .AspNetCore.Antiforgery.cookiedata
origin: https://localhost:44357
referer: https://localhost:44357/GeneralInterview/Index/16014
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36
x-requested-with: XMLHttpRequest
logoFiles: (binary)
logoFiles: (binary)
initialPreview: []
initialPreviewConfig: []
initialPreviewThumbTags: []

表单数据 files = logoFiles, I have just changed the name since then.

提前致谢。

【问题讨论】:

    标签: c# file-upload asp.net-core-mvc bootstrap-file-input


    【解决方案1】:

    在 ASP.NET Core 3.1 中使用 bootstrap-fileinput 插件测试

    查看

    name="files"必须与控制器参数名称List&lt;IFormFile&gt; files相同。

    @{
        Layout = null;
    }
    
    
    <div id="logoDiv">
        <input id="fileInput" name="files" type="file" class="file" data-allowed-file-extensions='["png", "jpg", "jpeg", "tiff"]' multiple data-theme="fas">
    
    </div>
    
    
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/5.1.2/css/fileinput.min.css" media="all" rel="stylesheet" type="text/css" />
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/5.1.2/js/plugins/piexif.min.js" type="text/javascript"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/5.1.2/js/plugins/sortable.min.js" type="text/javascript"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/5.1.2/js/plugins/purify.min.js" type="text/javascript"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" type="text/javascript"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/5.1.2/js/fileinput.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/5.1.2/themes/fa/theme.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/5.1.2/js/locales/(lang).js"></script>
    
    <script>
    
        $("#fileInput").fileinput({
            showCaption: false,
            showPreview: true,
            dropZoneEnabled: false,
            theme: "fa",
            browseClass: "btn btn-ey-upload btn-ey-utility",
            removeClass: "btn btn-ey-upload btn-ey-utility",
            uploadClass: "btn btn-ey-upload btn-ey-utility",
            showClose: false,
            uploadAsync: false,
            uploadUrl: "/GeneralInterview/UploadLogo",
            //uploadExtraData: { id: 1 }
        });
    
        // CATCH RESPONSE
        $('#fileInput').on('filebatchuploaderror', function (event, data, previewId, index) {
            var form = data.form, files = data.files, extra = data.extra,
                response = data.response, reader = data.reader;
        });
    
        $('#fileInput').on('filebatchuploadsuccess', function (event, data, previewId, index) {
            var response = data.response;
            alert(response);
        });
    
    </script>
    

    控制器

    这里我使用一个路由属性来匹配请求URL/GeneralInterview/UploadLogo

        [Route("/GeneralInterview/UploadLogo")]
        [HttpPost]
        public JsonResult UploadFile(List<IFormFile> files)
        {
            List<string> fileNames = new List<string>();
    
            foreach(IFormFile file in files)
                fileNames.Add(file.FileName);
            return Json(fileNames);
        }
    





    2020 年 4 月 9 日更新

    F12查看请求详情

    【讨论】:

    • 嗨莱特曼,感谢您的快速回复。不幸的是,即使在复制您的代码代替我的代码之后,我仍然会收到 400 错误。是否有可能是由于某些安全过滤器不喜欢 bootstrap-fileinput 的工作方式?我会将响应和请求标头粘贴到问题中。
    • 嗨,@AlexG,我没有在您的请求标头中看到表单数据,您可以检查您的输入视图和F12 以检查浏览器上的错误信息。
    • 我的表单数据和你的一模一样。
    • @AlexG,这是在新的 ASP.Net Core 3.1 MVCApplication 中添加的所有代码。这很奇怪
    • 我使用的是 ASP.NET Core 2.1 MVC,不知道会不会有什么不同?手动调用同一个端点确实会到达该方法(在我使用 GET 调用删除 HttpPost 标记之后)
    猜你喜欢
    • 2015-03-17
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 2019-12-11
    • 1970-01-01
    相关资源
    最近更新 更多