I'm trying to upload a file in my MVC project and when I pass the file to the controller via ajax it isn't working. When I check out the HttpPostedFileBase object that's passed to the controller everything is there bar the file. I can see that ReadTimeout and WriteTimeout are both giving the error "'file.WriteTimeout' threw an exception of type System.InvalidOperationException"
I've check out System.web and set the timeout property, as well as checking that the file is within the limit set. They're both fine. I've made sure that my post method is specifying enctype="form/multi-part data". This seems to be all I can find. Any suggestions would be much appreciated.
In my view it looks like this:
@using (Html.BeginForm("UploadDocument", "ControllerName", new { id = Url.RequestContext.RouteData.Values["id"] }, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table class="table table-condensed">
<tr>
<th>Upload Document</th>
<td class="text-right">
@Html.TextBoxFor(m => m.File, new { type = "file" })
</td>
</tr>
</table>
<input class="pull-right btn btn-success btn-sm" type="submit"
name="btnSubmit" value="Upload" id="btnSubmit">
}
Then in the controller:
[HttpPost]
public ActionResult UploadDocument(string id, HttpPostedFileBase file)
{
// Verify that the user selected a file
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
if (file != null)
{
if (model.File.ContentLength > 0)
{
var stream = file.InputStream;
var uploadResult = _client.module.Upload(custId, stream);
}
}
}
return RedirectToAction("NewAction", new { id = Guid.Parse(id) });
}
And finally webconfig:
<httpRuntime targetFramework="4.6.1" maxRequestLength="5120" executionTimeout="360"/>
The fie should be passed through as part of the HttpPostedFileBase parameter but it's not, it's just itming out and the local version of my API is showing that the parameter for the stream is null, which is to be expected since the file isn't making it to the controller. The file size and type are passed so I'm sure it's a timeout issue. The files I'm using to test are 1.2 and 2MB.
Any help would be much appreciated