代码之家  ›  专栏  ›  技术社区  ›  Zidane

在asp.net mvc中尝试上载文件时,如何解决mum请求长度超出错误

  •  0
  • Zidane  · 技术社区  · 6 年前

    我使用的是mvc,我试图上传一个9MB的文件,但我一直收到一个错误,在mycontroller中显示System.Web.Http.HttpResponseException


    <security>
          <requestFiltering>
            <!--<requestLimits maxAllowedContentLength="1073741824" />-->
            <requestLimits maxAllowedContentLength="3000000000" />
          </requestFiltering>
        </security>
    
    <system.web>
        <httpRuntime maxRequestLength="1048576" />
        <httpHandlers>
          <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    
        </httpHandlers>
    

    控制器

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Http;
    
    namespace Marketing_Database.Controllers
    {
    
        #region Multipart form provider class
        public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
        {
            public CustomMultipartFormDataStreamProvider(string path)
                : base(path)
            {
    
            }
    
            public override string GetLocalFileName(System.Net.Http.Headers.HttpContentHeaders headers)
            {
                string fileName;
                if (!string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName))
                {
                    fileName = headers.ContentDisposition.FileName;
                }
                else
                {
                    fileName = Guid.NewGuid().ToString() + ".data";
                }
                return fileName.Replace("\"", string.Empty);
            }
        }
        #endregion
    
        public class FileUploadController : ApiController
        {
            public Task<IEnumerable<string>> Post()
            {
                //throw new Exception("Custom error thrown for script error handling test!");
    
                if (Request.Content.IsMimeMultipartContent())
                {
                    //Simulate large file upload
                    System.Threading.Thread.Sleep(5000);
    
    
                    string fullPath = HttpContext.Current.Server.MapPath("~/Uploads");
                    CustomMultipartFormDataStreamProvider streamProvider = new CustomMultipartFormDataStreamProvider(fullPath);
                    var task = Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t =>
                    {
                        if (t.IsFaulted || t.IsCanceled)
                            throw new HttpResponseException(HttpStatusCode.InternalServerError);
    
                        var fileInfo = streamProvider.FileData.Select(i =>
                        {
                            var info = new FileInfo(i.LocalFileName);
                            return "File saved as " + info.FullName + " (" + info.Length + ")";
                        });
                        return fileInfo;
    
                    });
                    return task;
                }
                else
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "Invalid Request!"));
                }
            }
        }
    }
    
    0 回复  |  直到 6 年前
        1
  •  0
  •   Rupak    6 年前

    我认为你必须像这样包括目标框架。

    <system.web>
      <httpRuntime maxRequestLength="1073741824" targetFramework="4.5" />
    </system.web>
    
        2
  •  0
  •   hussein zakizadeh    6 年前
    <configuration>
      <system.web>
        <httpRuntime maxRequestLength="52428800" />
      </system.web>
    </configuration>