I have a form all setup to upload a file and that is working fine. However the way my form is submitted is through AJAX. The button that submits is still a type="submit" in case JS is off.
When I save my form the controller determines whether the IsAjaxRequest is true and if so returns some JSON otherwise it does a RedirectToAction.
When I don't specify a filepath in my input type="file" it considers IsAjaxRequest as true. If there is a filepath set then it thinks that IsAjaxRequest is false. How is it determining that?
My other problem is that when it thinks IsAjaxRequest is false and does a RedirectToAction("Index") I don't actually get sent to the Index view.
Thanks
Maybe the problem is not in the IsAjaxRequest()
? - it simply looks for ["X-Requested-With"] == "XMLHttpRequest"
in the incoming Request. Look at Mvc\AjaxRequestExtensions.cs:
public static class AjaxRequestExtensions {
public static bool IsAjaxRequest(this HttpRequestBase request) {
if (request == null) {
throw new ArgumentNullException("request");
}
return (request["X-Requested-With"] == "XMLHttpRequest")
|| ((request.Headers != null)
&& (request.Headers["X-Requested-With"] == "XMLHttpRequest"));
}
}