首頁  >  文章  >  web前端  >  JS下載檔案|無刷新下載檔案範例程式碼_javascript技巧

JS下載檔案|無刷新下載檔案範例程式碼_javascript技巧

WBOY
WBOY原創
2016-05-16 16:52:161324瀏覽

後台程式碼Handler.ashx

複製程式碼 程式碼如下:



using System;
using System.Web;

public class Handler : IHttpHandler {

public void ProcessRequest (HttpHContext ) {
string fileName = "web.config";//客戶端儲存的檔案名稱
string filePath = context.Server.MapPath("web.config");//路徑
//以字元流的形式下載檔案
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";
////通知瀏覽器下載檔案而不是開啟
context.Response.AddHeader("Content-Disposition", "attachment; filename=" HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
contextcode. Response.BinaryWrite(bytes);
context.Response.Flush();
context.Response.End();
}

public bool IsReusable {
}

public bool IsReusable {
} return false;
}
}
}

前端程式碼: >
代碼如下:









<script> <BR>function download_file( url) <br>{ <br><BR>if (typeof (download_file.iframe) == "undefined") <BR>{ <BR>var iframe = document.createElement("iframe"); <BR>var iframe = document.createElement("iframe"); <BR>download_file. iframe = iframe; <BR>document.body.appendChild(download_file.iframe); <BR>} <br>// alert(download_file.iframe); <br>download_file.iframe.src = url; <br>🎜>🎜 = url; >download_file.iframe.style.display = "none"; <br><br><br><BR>} <BR></script>


aaaaa
bbbbb
ccccc

身體>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn