Ajax Download files

Source: Internet
Author: User
Tags file url

Originally the file can be downloaded directly through hyperlinks, but now to include a few parameters in the URL, and these parameters if clicked event triggered by the latest value, so the use of Ajax in the way to download

However:

1. The return value type using Ajax,ajax is json,text,html,xml type, or it can be said that Ajax send, accept can only be string string, cannot stream type, so can not implement file download, strong use will appear response conflict.

If you want to use Ajax, you can only return file-related URLs that are worth generating. The callback function is then created by creating an IFRAME and setting its SRC value to the file URL, or a processing URL to the file generation stream, so that the file download is done and the page is not refreshed.

2. Do not use Ajax, through the DOM dynamic operation or create iframe,form way to achieve, while downloading the file implementation of the page is not refreshed, where the SRC can be a file address URL to download files directly, can also be a stream processing URL through the response stream output download, form is the stream processing URL through the response stream output download, Dom dynamic operation when the file download, and the page does not refresh.


Here is the use of 22 operation of the DOM way to achieve the download:

Operation iframe Mode:

function Ajaxdownload (urlpost,data) {       $.ajax ({url:urlpost,type: "POST", Cache:false,data:data,beforesend: function () {        $ ("#grid_crud"). Pqgrid ("showloading")        },success:function (filename) {var url = urlpost + (( Urlpost.indexof ("?") > 0)? "&": "?") + $.param (data)); $ (document.body). Append ("<iframe height= ' 0 ' width= ' 0 ' frameborder= ' 0 '  src=" + URL + "></ Iframe> ")},complete:function (data) {        $ (" #grid_crud "). Pqgrid (" hideloading ")        },        error:function (A, B , c) {        $ ("#grid_crud"). Pqgrid ("hideloading")        }});       

Operation form:

        function Ajaxdownload (url,data,method) {        if (URL && data) {        //data is string or array/object        data = typeof data = = ' string '? Data:jQuery.param (data);        var inputs = ';        Jquery.each (Data.split (' & '), function () {        var pair = this.split (' = ');        Inputs + = ' <input type= "hidden" name= "' +pair[0]+ '" value= "' +pair[1]+ '"/> ";        });        JQuery (' <form action= ' +url+ ' "method=" ' + (Method | | ' Post ') + ' > ' +inputs+ ' </form> '). AppendTo (' body '). Submit (). remove ();}        

 

Ajax Download files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.