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:
Client:
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);
}
}
Server:
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:
<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>
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With