0

I have a form that does a http-get which returns a file. If I simply use the in-built get from form the browser attempts to download the file, which is what I want. However, I want to do the http-get through jQuery so that I can display a loading bar that terminates when the get has finished its request. (Request takes ~20 seconds)

I do the request like this:

 $('#dagens').on('submit', function (event) {
    event.preventDefault();
    nanoBar.go(50);
    console.log("NProgressShouldHaveStarted")

    $.get( $(this).attr('action'), function( data ) {
        $( ".result" ).html( data );
            console.log( "Loadwasperformed.");
            nanoBar.go(100);
    });
  });

I gather that the file resides in 'data', but how can I pass this to the browser so that it prompts the user to download it?

LoudRobed
  • 11
  • 1
  • is data the files content or url? – Exlord Sep 10 '14 at 10:54
  • The file's content. If I do $('#view').html(data); the file content is dumped to the browser as text. (It's a csv-file) – LoudRobed Sep 10 '14 at 10:59
  • duplicate of http://stackoverflow.com/questions/13548102/creating-download-prompt-using-purely-javascript – Exlord Sep 10 '14 at 11:02
  • I see, but content-disposition and contentType is set server side and I don't really WANT to store it as a variable, I just want the jQuery-get to do the same as if I just do
    " id="dagens" >
    In which case it downloads as you would expect.
    – LoudRobed Sep 10 '14 at 11:15
  • in that case you can try submitting the form to a hidden iframe, see what happens... i am not sure but i think that the hidden iframe will promote for download – Exlord Sep 11 '14 at 04:15
  • I ended up using a javascript lib called fileDownload.js ( http://jqueryfiledownload.apphb.com/ ) Which does exactly that, with a cookie for determining when the request is finished. – LoudRobed Sep 15 '14 at 14:13

0 Answers0