Reputation:
Ok, this is driving me nuts. I have been trying to figure this out forever now. I want to to perform an async upload using ajaxFileUpload. I have it posting the file to my controller just fine, however, when i try to return a JsonResult from it, I get a "Save File As" dialog box. I have tried everything to get around this and i either get an error or the "Save As" dialog. Im assuming that it has something to do with the content type that is being passed in when the file gets posted. I would be greatly appreciative if anyone could help. Thanks in advance!
Upvotes: 1
Views: 2605
Reputation: 11
In your controller only include "text/x-json" like this:
public JsonResult UploadFile(FormCollection form, ...
return Json("{id: xxxx, text: 'bla bla bla'}", "text/x-json");
Upvotes: 1
Reputation: 10239
If you look at the code there, it's actually creating a new form and iframe, and then POSTing the form to the iframe. The response is written back to the iframe, which the code then picks up and returns. I'd assume if you loaded the JsonResult directly in a browser it would also cause the same download box to pop up. Check the headers that are being returned with the JsonResult to see why when being loaded as a page it thinks its a download. Maybe you need to set the Content-Type as text/plain or remove a Content-Disposition header.
Upvotes: 1
Reputation: 51
I had the same problem and I solved it with
public ContentResult UploadFile(FormCollection form, ...
return Content("{success: false, ErrorMsg: 'someerror'}");
Upvotes: 5