views:

797

answers:

2

Is it possible to make an ajax call (using Jquery) to request a file.

(The ajax call passes back a chunk of information) the Server reponds by sending a file or an error message.

If the server sends the file I would like the browser to handle the request so you get the browser's standard file download action.

Is that doable? Or is it only possible to answer ajax calls with data responses?

Currently I'm returning the following from the controller:

return File(report, "application/pdf","test.pdf");

(Where report is a byte array)

This is sent ok but is handled by the brower's XMLHttpRequest not by the browser.

Annoyingly for various reasons I can't use a form post.

A: 

The only way to give a file upload ajax feeling is to post the form thru a hidden iframe

jQuery("#yourFormID").submit(function(data){
     var submittingForm = jQuery( this );

     var frameName = ("upload"+(new Date()).getTime());
     var uploadFrame = jQuery("<iframe name=\""+frameName+"\" id=\""+frameName+"\" />");
     submittingForm.attr("target", frameName);

     uploadFrame.load(function(data){
      var data_found = jQuery("#"+frameName).contents().find("*>body").html() + ')');
        });
     jQuery("body:first").append(uploadFrame);
    });

The form tag needs none spezific information only (ID and Action)

funktioneer
Reread the question. Wilfred is doing file download.
RichardOD
+1  A: 

Is your question the same as this one?

http://stackoverflow.com/questions/676348/allow-user-to-download-file-using-ajax

jayrdub
Well spotted I don't know why I couldn't find that!
Wilfred Knievel