-1

I realize that variations on this question have been asked before. The best answer I have found is at

File download in Asp.Net MVC 2

But trying to follow those instructions did not solve the problem for me.

Long story short, the file is being retrieved correctly, the name, path and mime type are all correct, and no errors are thrown. No errors are thrown by the javascript on the client-side either.

The C# code that gets the file looks like this:

[HttpPost]
public FileResult DownloadFile(int fileId)
{
    ... get the file and file info
    return File(fileBytes, fileMimeType, fileName);
}

The javascript looks like this:

... set up for post here
$.post(settings.actions.downloadFile, {fileId: fileIdVar});

As I was saying, the post returns and nothing happens.

I have tried changing the post to a get, and the result was the same.

I have tried setting up a callback function that sets document.location.href to some random url on return from the download, but that just takes my browser to the page I specified. I cannot understand, from the explanation given in the link I provided, that is

"...Use document.location.href = ... to tell the browser to go to the url for downloading the file. It'll see the content disposition header and will display it as a download not as a page..."

What I'm supposed to point my browser to. document.location.href doesn't accept data, so I can't use it on its own, and using post without document.location.href returns nothing.

What could I be doing wrong?

Big thanks to responders for their time!

Community
  • 1
  • 1
justian17
  • 575
  • 1
  • 7
  • 17
  • I believe the problem is that the POST is done via AJAX. If you do a standard post, you probably will be able to download the file without a problem – Andy T Dec 10 '13 at 16:51
  • That sounds like a good idea, Queti, but how would I do that from within javascript? That is, how do I post without using $.post, which uses ajax? Are you saying that I should set up a different mechanism, such as a form in my html? – justian17 Dec 10 '13 at 17:06
  • Yes, set up a form with the POST method. If you still need to do some sort of validation or modifying data before the form gets sent to the server, you can still do that, and you simply trigger the form to submit using the `$(form).submit()`. – Andy T Dec 10 '13 at 17:12
  • Thank you again for your help, Queti. As a question for the sake of completeness, would it be possible to dynamically create a form that lets me download a file and then submit that form inside of my javascript? – justian17 Dec 10 '13 at 18:47
  • What's the downvote for? Wouldn't be ... political...would it? That would be silly. – justian17 Jun 30 '15 at 11:12

1 Answers1

1

Just like the answer in the post you linked says, you can't download a file via AJAX.

In order to set the location, change your action to respond to GET requests and either add your file id to the query string or setup a route to handle it. Also, and you may already be doing this, but you'll need to make sure you set the Content-Dispostion header value to attachment.

window.location.href = settings.action.downloadFile + "?fileId=" + fileIdVar

Since you are using jQuery, you could use $.param to build the parameters for you.

You could also look into a plugin to provide an "AJAX like" experience.

Community
  • 1
  • 1
Chris
  • 4,393
  • 1
  • 27
  • 33
  • Thanks for your help, Rfvgyhn! What ended up working for me was to change the document.location.href, as you suggested without using the ajax call. This line correctly lets me download the file: document.location.href = settings.actions.downloadFile + "?fileId=" + fileIdVar; – justian17 Dec 10 '13 at 18:41