无法使用MVC.NET Core应用程序上传大文件

时间:2018-12-07 03:47:17

标签: c# asp.net-core-mvc

我在MVC.NET Core应用程序中上传大文件时遇到问题。无论我做什么,我都会返回404无数据响应

这是我上传文件的任务方法

        [RequestSizeLimit(40000000)]
    public static async Task<string> UploadAttachment(string bis232JsonString, string url, string formType, string token, long maxResponseContentBufferSize)
    {
        var result = "Error";
        try
        {
            // To Do : Use ConfigurationManager.AppSettings["endpoint"];
            string endpoint = $"{url}/{formType}Attachment/post";

            using (var client = new HttpClient())
            {

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                //client.MaxResponseContentBufferSize = maxResponseContentBufferSize;

                using (var request = new HttpRequestMessage(HttpMethod.Post, endpoint))
                {
                    request.Content = new StringContent(bis232JsonString, Encoding.UTF8, "application/json");
                    using (var response = await client.SendAsync(request))
                    {
                        if (response.IsSuccessStatusCode && (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.NoContent))
                            result = "Success";
                        else
                            result = "Error";
                    }
                }
            }
        }
        catch (Exception ex)
        {
            //to do : Log application error properly
            result = "Error";
        }

        return result;
    }

我尝试了以下

  1. 试图使用HTTP客户端的MaxResponseContentBufferSize属性
  2. 试图使用[RequestSizeLimit(40000000)]装饰器
  3. 试图使用[DisableRequestSizeLimit]装饰器
  4. 尝试如下所示全局更新上传限制

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseKestrel(options =>
        {
            options.Limits.MaxRequestBodySize = 52428800; });
    

但是,无论我尝试做什么,似乎都没有效果

请让我知道是否有什么办法可以使它起作用

0 个答案:

没有答案