我试过在这里搜索和谷歌寻找答案但尚未找到答案。我使用我发现的相当标准的.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);
}
当我使用input 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文件夹,然后在那里看到部分文件。这个小部件也不总是相同的尺寸,例如它可能会切断一定的尺寸。
我已将maxRequestLength
和maxAllowedContentLength
调整为大数,但没有成功。
这条消息意味着什么,我应该注意什么来修复它?
感谢。