JS Download file | No refresh download file Sample code _javascript tips

Source: Internet
Author: User
Background Code HANDLER.ASHX
Copy Code code as follows:

<%@ WebHandler language= "C #" class= "Handler"%>

Using System;
Using System.Web;

public class Handler:ihttphandler {

public void ProcessRequest (HttpContext context) {
string filename = "Web.config";//client saved file name
String FilePath = context. Server.MapPath ("web.config");//path
Downloading files as streams of characters
System.IO.FileStream fs = new System.IO.FileStream (FilePath, System.IO.FileMode.Open);
byte[] bytes = new byte[(int) fs. Length];
Fs. Read (bytes, 0, bytes. Length);
Fs. Close ();
Context. Response.ContentType = "Application/octet-stream";
Notifies the browser to download the file instead of opening it
Context. Response.AddHeader ("Content-disposition", "attachment; Filename= "+ httputility.urlencode (FileName, System.Text.Encoding.UTF8));
Context. Response.BinaryWrite (bytes);
Context. Response.Flush ();
Context. Response.End ();
}

public bool IsReusable {
get {
return false;
}
}

}

Front-End Code:
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">


<script src= "Jquery-1.7.2.min.js" type= "Text/javascript" ></script>
<title></title>
<script>
function Download_file (URL)
{

if (typeof (download_file.iframe) = = "undefined")
{
var iframe = document.createelement ("iframe");
Download_file.iframe = iframe;
Document.body.appendChild (Download_file.iframe);
}
alert (download_file.iframe);
download_file.iframe.src = URL;

Download_file.iframe.style.display = "None";



}
</script>
<body>
<a href= "javascript:void (0);" onclick= "Download_file (' handler.ashx ')" >aaaaa</a>
<a href= "javascript:void (0);" onclick= "Download_file (' handler.ashx ')" >bbbbb</a>
<a href= "javascript:void (0);" onclick= "Download_file (' handler.ashx ')" >ccccc</a>

</body>

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.