简体   繁体   English

在asp net core web api中上传文件时,有没有办法避免http错误405?

[英]Is there a way to avoid http error 405 when uploading a file in asp net core web api?

I have an AJAX form which sends data (file uploads) to an ASP NET Web API using POST.我有一个 AJAX 表单,它使用 POST 将数据(文件上传)发送到 ASP NET Web API。 I can hit my API end point without a problem and save the files and then return an OK.我可以毫无问题地点击我的 API 端点并保存文件,然后返回 OK。 When the execution finishes on the API my page now display an error saying 405 method not allowed .当 API 上的执行完成时,我的页面现在显示一个错误,说405 method not allowed I have configured CORS and allowed access to any origin also I have tried uploading the files using chrome with security disabled just to make sure its not CORS but still I am getting a Method not allowed when the code executes without a problem.我已经配置了 CORS 并允许访问任何来源我也尝试使用禁用安全性的 chrome 上传文件只是为了确保它不是 CORS 但当代码执行没有问题时我仍然得到一个不允许的方法。

Below is my AJAX.下面是我的 AJAX。

  <script>

    function uploadFiles() {    

        var fileUpload = $("#files").get(0);
        var files = fileUpload.files;
        var data = new FormData();

        for (var i = 0; i != files.length; i++) {
            data.append("files", files[i]);
        }
        $.ajax({
            type: "POST",

            url: "@TempData["api"]api/Document/BulkUpload/",
            headers: {
                'Authorization': 'Bearer @HttpContextAccessor.HttpContext.Session.GetString("token")'
            },

            contentType: false,
            processData: false,
            data: data,
            async: false,
            beforeSend: function () {
                //Before sending
            },
            success: function (messages) {
                var list = messages;
                $.each(list, function (index, item) {
                    //alertify.set('notifier', 'position', 'top-center');
                    //alertify.success('Successfully uploaded the Document');
                    alert(item);
                });

                let delay = 5000;
                let url = "/Documents";
                setTimeout(function () {

                    location = url;

                }, 5000);
            },
            error: function (e) {
                console.log(e.responseText);
                if (e.responseText == null) {
                    e.responseText = "Failed to upload Document. Contact the admin";
                }
                alertify.set('notifier', 'position', 'top-center');
                alertify.error(e.responseText);
            },
            complete: function () {
               //After sending
            }
        });

    }

</script>

Below is my API end point:下面是我的 API 端点:

[HttpPost]
    [Route("BulkUpload")]
    public async Task<ActionResult> BulkUpload(IList<IFormFile> files)
    {
        //Errors
        List<string> Errors= new List<string>();

        var documents = files;

//Do whatever with the docs

}

Below are my CORS settings:以下是我的 CORS 设置:

            app.UseCors(x => x
        .AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader());

Below is my web.config file:下面是我的 web.config 文件:

    <system.webServer>
    <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
            <add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, OPTIONS" />
            <add name="Access-Control-Max-Age" value="1000" />
        </customHeaders>
    </httpProtocol>
</system.webServer>

Is there anything I am doing wrong?有什么我做错了吗?

After successful upload, you are setting the current location to the URL上传成功后,您将当前位置设置为 URL

/Documents /文件

It seems like the corresponding endpoint does not support HTTP GET method allowed.似乎相应的端点不支持HTTP GET方法允许。 Allowing the GET method allowed to that endpoint should resolve your issue.允许该端点允许的GET方法应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Web API REST Service: HTTP ERROR 405 - ASP.NET Web API REST Service: HTTP ERROR 405 ASP.NET Core 5.0 Web API 在尝试使用 HttpDelete 删除记录时抛出错误 405 - ASP.NET Core 5.0 Web API throws error 405 when trying to delete record using HttpDelete 上传和处理.csv文件到ASP.NET内核Web ZDB974238714CA8DE634A7CE1D08 - Uploading and handling .csv file to an ASP.NET Core Web API 在发布对象数组时收到来自 Web API 的 405 HTTP 响应(.Net Core 5) - Receiving a 405 HTTP Response from Web API when posting an array of objects (.Net Core 5) HTTP 错误 405 | [HttpPost] ASP.NET MVC 核心 3.1 - HTTP ERROR 405 | [HttpPost] ASP.NET MVC Core 3.1 ASP.NET Core 3.1 Web API post 方法导致 405“Method Not Allowed”错误 - ASP.NET Core 3.1 Web API post method causing 405 "Method Not Allowed" error 在 IIS 上上传文件时,ASP.NET Core API 500 内部服务器错误 - ASP.NET Core API 500 Internal Server Error when uploading file on IIS ASP.NET 内核 HTTP 错误 405 - 路由到 controller 操作后的错误方法 - ASP.NET Core HTTP Error 405 - Bad Method when routing to post controller action HTTP 错误:405(此页面不工作)当我刷新页面时(ASP.Net Core MVC) - HTTP ERROR : 405(This page isn't working) When I Refresh The Page(ASP.Net Core MVC) ASP.NET Web API-尝试传递参数时不支持GET HTTP Verb(405) - ASP.NET Web API - GET HTTP Verb not supported (405) when attempting to pass a parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM