網頁下載方法:常見
- 使用<a>網址下載的方式。
- 透過事件的方式來處理。比如click事件後,觸發下載檔案的方法。
如果要資料來源為資料庫或是要透過程式處理一些邏輯,
常見作法是使用Response.Write處理檔案的標頭內容等資訊,
但直接處理會破壞原先網頁的結構,
所以常透過泛型檔(.ashx)或另一個頁面(.aspx)來處理檔案下載的部分。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.IO; | |
using System.Text; | |
namespace Web | |
{ | |
/// <summary> | |
///downloadFile 的摘要描述 | |
/// </summary> | |
public class downloadFile : IHttpHandler | |
{ | |
public void ProcessRequest(HttpContext context) | |
{ | |
this.TestDownloadFile(); | |
} | |
/// <summary> | |
/// 測試下載檔案 | |
/// </summary> | |
private void TestDownloadFile() | |
{ | |
HttpContext.Current.Response.ContentType = "text/plain"; | |
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + string.Format("text-{0}.txt", string.Format("{0:yyyyMMddHHmmss}", DateTime.Today))); | |
HttpContext.Current.Response.Clear(); | |
using (StreamWriter writer = new StreamWriter(HttpContext.Current.Response.OutputStream, Encoding.UTF8)) | |
{ | |
writer.Write("This is the content"); | |
} | |
HttpContext.Current.Response.End(); | |
} | |
public bool IsReusable | |
{ | |
get | |
{ | |
return false; | |
} | |
} | |
} | |
} |
在此要處理另外一個頁面處理下載檔案,可能會使用開窗或是導頁的方法。
- 開窗:可能會受到瀏覽器設定限制,阻擋廣告等機制。
- 導頁:會跑到另外一個頁面,但設計上可能不希望使用者離開原頁面。
網路上也提供另外一種做法,就是透過iframe的方式下載。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Web.Default" Theme="Red" %> | |
<%@ Register src="usercontrol1.ascx" tagname="usercontrol1" tagprefix="uc1" %> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head runat="server"> | |
<title></title> | |
</head> | |
<body> | |
<form id="form1" runat="server"> | |
<div> | |
<asp:ScriptManager ID="scriptmanager1" runat="server"> | |
</asp:ScriptManager> | |
<asp:Button ID="btnDownload" runat="server" Text="下載" onclick="btnDownload_Click" /> | |
<div style="display:none"> | |
<!--iframe不可以display:none,可以在外面包div隱藏--> | |
<iframe id="iframeDownload" runat="server" style="width:0;height:0"></iframe> | |
</div> | |
</div> | |
</form> | |
</body> | |
</html> |
程式碼如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.UI; | |
using System.Web.UI.WebControls; | |
namespace Web | |
{ | |
public partial class Default : System.Web.UI.Page | |
{ | |
/// <summary> | |
/// 網頁讀取 | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
if (Page.IsPostBack) | |
return; | |
} | |
/// <summary> | |
/// 點擊[下載] | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
protected void btnDownload_Click(object sender, EventArgs e) | |
{ | |
//下載網址 | |
string downloadUrl = new Uri(new Uri(Request.Url.AbsoluteUri),"/downloadFile.ashx").AbsoluteUri; | |
//註冊下載網址給iframe | |
this.iframeDownload.Attributes.Add("src", downloadUrl); | |
//重整後網址是否清空,或其他邏輯再依照其他狀況處理 | |
} | |
} | |
} |