┌─────────┐ ┌─ ───────────┐ ┌───────────────────────┐
│ Postman │ ───► │ Web API App │ ───► │ Save file to a folder │
└─────────┘ └─────────────┘ └───────────────────────┘
In order to simulate, I stream a file to Web API via postman and the API finally saves the file to a folder.
Problem - input.Read
throws Maximum request length exceeded.
exception.
Question - Could I stream upload a large file without adding maxRequestLength and maxAllowedContentLength in web.config?
In other words, do we have any work around without adding those settings in web.config
?
public class ServerController : ApiController
{
public async Task<IHttpActionResult> Post()
{
// Hard-coded filename for testing
string filePath = string.Format(@"C:\temp\{0:yyyy-MMM-dd_hh-mm-ss}.zip", DateTime.Now);
int bufferSize = 4096;
int bytesRead;
byte[] buffer = new byte[bufferSize];
using (Stream input = await Request.Content.ReadAsStreamAsync())
using (Stream output = File.OpenWrite(filePath))
{
while ((bytesRead = input.Read(buffer, 0, bufferSize)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
return Ok();
}
}