ASP. NET WEB Ajax request ashx Service implementation file download

Source: Internet
Author: User

For specific functions, select a row in the File data list, click the Download button above the table to download the file. Because the table does not contain the file path, can only take the ID in the database table, so the concrete implementation is this: first click on a row, get the ID of a row of data clicked, with the value of Ajax to the ASHX service file, in ASHX based on the ID to take the file path, Return to the Web page after converting to the server virtual path. Download with Ajax in a Web page.

Initially tried directly in the ASHX Service Processing response.writefile (), the results could not be downloaded, possibly because of the AJAX request, and return to the Ajax callback function directly. So we have to take the path of the file directly back to the callback function, and then on the callback function road download.

Find a variety of online download methods, using jquery to deal with the file path is the most recommended is Window.href=url, perhaps a lot of demand is not download files, but redirect it. If the download file is also possible to write, but the user experience is not good, click on the download after the jump page, download is still going back or what. So I didn't use it that way.

Let's talk about the method I use, more ingenious. is to first add a hidden a tag to the page, hide it, assign a value to the href of a when getting to the file path, and then trigger the Click event on the a tag.

<id= "DP"  href= ""  style= "Display:none;" ><  ID= "fp"></span></a  >

Here a tag is not used if it is triggered directly, so a span tag is added to it, and the click event of the triggered span tag is available.

function Download () {//When you select a row, assign a value to the ROWID.            varID = $ ("#rowid"). Val (); $.ajax ({type:"POST", URL:"download.ashx", DataType:"JSON", data: {action:"DownLoad", Id:id}, Success:function (data) {if(Data! =NULL|| Data! ="") {                        //gets the file path returned                        varPath = data[0]["Path"].tostring (); //assign a value to a tag$("#dp"). attr ("href", path); //The FP here is the ID of the span tag in the A tag$("#fp"). Click (); }                    Else{alert ("file does not exist");        }                }            }); }

This is really a practical little trick, this will no longer worry about the Ajax file download problem.

ASP. NET WEB Ajax request ashx Service implementation file download

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.