尝试使用HttpClient上传文件时来自Web Api的RequestEntityTooLarge响应 [英] RequestEntityTooLarge response from Web Api when trying to upload a file with HttpClient
问题描述
我正在尝试创建一个Web服务,以通过Asp.Net Web Api通过Post上传文件.这些分别是Client和Web Api的实现:
I'm trying to create a web service to upload files by Post with Asp.Net Web Api. These are the implementations for Client and Web Api respectively:
客户:
using (var client = new HttpClient()) {
client.BaseAddress = new Uri("https://127.0.0.1:44444/");
using (var content =
new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture))) {
content.Add(new ByteArrayContent(File.ReadAllBytes(filepath)));
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
var response = await client.PostAsync("api/Synchronization", content);
if (response.IsSuccessStatusCode)
eventLog1.WriteEntry("Synchronization has been successful", EventLogEntryType.Information);
else
eventLog1.WriteEntry(response.StatusCode + ":" + response.ReasonPhrase, EventLogEntryType.Error);
}
}
服务器:
public class SynchronizationController : ApiController {
public HttpResponseMessage SynchronizeCsv() {
var task = this.Request.Content.ReadAsStreamAsync();
task.Wait();
Stream requestStream = task.Result;
try {
Stream fileStream = File.Create(HttpContext.Current.Server.MapPath(path));
requestStream.CopyTo(fileStream);
fileStream.Close();
requestStream.Close();
}
catch (IOException) {
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "A generic error occured. Please try again later.");
}
HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.Created;
return response;
}
}
Web.config:
Web.config:
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" maxRequestLength="2097152" />
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483648" />
</requestFiltering>
</security>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
我正在尝试上传一个5Mb的文件,每次尝试调用该服务时,都会收到一个413 Http错误请求实体太大的消息.如果文件较小(例如40kb),则一切正常.
I'm trying to upload a file which is 5Mb, and every time I try to call the service, I get a 413 Http Error Request Entity Too Large. If the file is small (e.g. 40kb) everything works fine.
通过Internet查看,我尝试对web.config进行一些修改,但似乎没有任何正常工作.我在做什么错了?
Looking through internet I tried to do several modifications to the web.config, but nothing seems to work properly. What am I doing wrong?
推荐答案
I solved the problem through this solution: http://www.smartdigitizers.com/net-development-tips/iis-7-how-to-resolve-error-https-413-request-entity-too-large/
- 启动"Internet信息服务(IIS)管理器"
- 选择要在其下托管Web应用程序的网站.
- 在功能"部分中,双击配置编辑器"
- 在部分"下,选择:system.webServer,然后serverRuntime
- 将"uploadReadAheadSize"部分修改为20MB(该值以字节为单位)
- 点击应用"
这篇关于尝试使用HttpClient上传文件时来自Web Api的RequestEntityTooLarge响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!