I am using mvc c# and I am trying to upload a file that is 9MB but I keep getting an error that says System.Web.Http.HttpResponseException in mycontroller I am not sure what is causing this error. I have done my research and I found nothing that actually helps I have also edited my web config as shown below but I am not sure if its correct.
<security>
<requestFiltering>
<!--<requestLimits maxAllowedContentLength="1073741824" />-->
<requestLimits maxAllowedContentLength="3000000000" />
</requestFiltering>
</security>
<system.web>
<httpRuntime maxRequestLength="1048576" />
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
Controller
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
namespace Marketing_Database.Controllers
{
#region Multipart form provider class
public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
{
public CustomMultipartFormDataStreamProvider(string path)
: base(path)
{
}
public override string GetLocalFileName(System.Net.Http.Headers.HttpContentHeaders headers)
{
string fileName;
if (!string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName))
{
fileName = headers.ContentDisposition.FileName;
}
else
{
fileName = Guid.NewGuid().ToString() + ".data";
}
return fileName.Replace("\"", string.Empty);
}
}
#endregion
public class FileUploadController : ApiController
{
public Task<IEnumerable<string>> Post()
{
//throw new Exception("Custom error thrown for script error handling test!");
if (Request.Content.IsMimeMultipartContent())
{
//Simulate large file upload
System.Threading.Thread.Sleep(5000);
string fullPath = HttpContext.Current.Server.MapPath("~/Uploads");
CustomMultipartFormDataStreamProvider streamProvider = new CustomMultipartFormDataStreamProvider(fullPath);
var task = Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t =>
{
if (t.IsFaulted || t.IsCanceled)
throw new HttpResponseException(HttpStatusCode.InternalServerError);
var fileInfo = streamProvider.FileData.Select(i =>
{
var info = new FileInfo(i.LocalFileName);
return "File saved as " + info.FullName + " (" + info.Length + ")";
});
return fileInfo;
});
return task;
}
else
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "Invalid Request!"));
}
}
}
}