开发者

Can jQuery retrieve a file?

开发者 https://www.devze.com 2023-01-02 15:45 出处:网络
I have a django application that creates xls and txt files. I\'m trying to create a button which sends a jQuery.get request to django, django then returns a freshly created file to jQuery which in tu

I have a django application that creates xls and txt files.

I'm trying to create a button which sends a jQuery.get request to django, django then returns a freshly created file to jQuery which in turn pops open a save as dialog.

My code looks like this:

jQuery("#testButton").live("click",function()
{
    jQuery.jGrowl("click");
    jQuery.get("/filetest",function(data){});

});


<div id="testButton" class="button">CLICK TO TEST</div>

Getting the file to jQuery is easy, but I have no idea how to then r开发者_JAVA技巧aise a Save As dialog.

Any assistance would be fantastic!


you can use iframe in your page and change with jquery the source, to the file you want to download.

this code put in html body :

<iframe id="download" style="width:0px;height:0px;display:none;"></iframe>

and the jquery in event of click look like:

$('#download').attr("src",location.href+"&make_excell=1");

in your sever side send the file back when the request make_excell with a content-type


How about a window.open invocation which opens a server-side file that sends a Content-Type of a file attachment along with the file contents in the Content Body?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号