2

我曾尝试在此处和 Google 上搜索此问题的答案,但尚未找到。我正在使用我发现的相当标准的 .NET 4.0 上传到 Web API 服务。这是代码:

    public HttpResponseMessage Post()
    {
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }
        StringBuilder sb = new StringBuilder();

        string root = HttpContext.Current.Server.MapPath("~/App_Data");
        var provider = new MyMultipartFormDataStreamProvider(root);

        var task = Request.Content.ReadAsMultipartAsync(provider).ContinueWith<HttpResponseMessage>(t =>
        {
            if (t.IsFaulted || t.IsCanceled)
            {
                Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
            }

            // This will give me the form field data
            foreach (var key in provider.FormData.AllKeys)
            {
                foreach (var val in provider.FormData.GetValues(key))
                {
                    sb.Append(string.Format("{0}: {1}", key, val));
                }
            }

            // This will give me any file upload data
            foreach (MultipartFileData file in provider.FileData)
            {
                sb.Append(file.Headers.ContentDisposition.FileName);
                sb.Append("Server file path: " + file.LocalFileName);
            }
            return new HttpResponseMessage()
            {
                Content = new StringContent(sb.ToString())
            };
        });
        return Request.CreateResponse(HttpStatusCode.OK);
    }

当我使用输入 type=file 创建一个非常基本的表单并提交它时,我收到超过 800Kb 文件的异常。这是一个例外:

System.ArgumentException was unhandled by user code
  HResult=-2147024809
  Message=Value does not fall within the expected range.
  Source=mscorlib
  StackTrace:
       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
       at System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(String name)
       at System.Web.Hosting.IIS7WorkerRequest.GetServerVariable(String name)
       at System.Web.Hosting.IIS7WorkerRequest.GetRemoteAddress()
       at System.Web.HttpWorkerRequest.IsLocal()
       at System.Web.Configuration.CustomErrorsSection.CustomErrorsEnabled(HttpRequest request)
       at System.Web.HttpContextWrapper.get_IsCustomErrorEnabled()
       at System.Web.Http.WebHost.HttpControllerHandler.<>c__DisplayClassa.<ConvertRequest>b__9()
       at System.Lazy`1.CreateValue()
       at System.Lazy`1.LazyInitValue()
       at System.Lazy`1.get_Value()
       at System.Web.Http.HttpConfiguration.ShouldIncludeErrorDetail(HttpRequestMessage request)
       at System.Net.Http.HttpRequestMessageExtensions.CreateErrorResponse(HttpRequestMessage request, HttpStatusCode statusCode, Func`2 errorCreator)
       at System.Net.Http.HttpRequestMessageExtensions.CreateErrorResponse(HttpRequestMessage request, HttpStatusCode statusCode, Exception exception)
       at aocform.Controllers.ValuesController.<>c__DisplayClass2.<Post>b__1(Task`1 t) in c:\Users\fred_malone\Documents\Visual Studio 2012\Projects\aocform\aocform\Controllers\ValuesController.cs:line 30
       at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
       at System.Threading.Tasks.Task.Execute()
  InnerException: 

我检查了 App_Data 文件夹,并在那里看到了文件的一部分。这个小部分也不总是相同的大小,就像它可能在某个大小处切断一样。

我已经将 themaxRequestLength和 the调整maxAllowedContentLength为大数字,但没有成功。

此消息是什么意思,我应该如何解决它?

谢谢。

4

0 回答 0