用页传值方式解决模态窗口的Response.WriteFile文件下载
生活随笔
收集整理的這篇文章主要介紹了
用页传值方式解决模态窗口的Response.WriteFile文件下载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
因為項目需要,使用了模態窗口,故在BasePage中的override void OnInit(EventArgs e)中加入如下內容.
????????????Response.Clear();
????????????Response.Buffer?=?true;
????????????Response.ExpiresAbsolute?=?System.DateTime.Now.AddSeconds(-1);
????????????Response.Expires?=?0;
????????????Response.CacheControl?=?"no-cache";
????????????string?nocache?=?"<meta?http-equiv=\"pragma\"?content=\"no-cache\">\n";
????????????nocache?+=?"<meta?http-equiv=\"cache-control\"?content=\"no-cache,?must-revalidate\">\n";
????????????nocache?+=?"<meta?http-equiv=\"expires\"?content=\"WED,?26?Feb?1997?08:21:57?GMT\">\n";
????????????Response.Write(nocache); ?可因為一個需求,需要在模態窗口中添加附件的下載功能,因為對附件個數的不確定,所以剛開始的解決方法是直接將附件路徑寫在<a href></a>里,單擊直接下載,使用過程中遇到了問題,譬如,當文件是圖片或者是TXT文件是,IE瀏覽器是直接在線打開的.而我要求是點擊后提示是否保存或者打開(注:模態窗口禁用了右鍵,所以沒有另存為).
??????怎樣才能讓IE出現提示呢?我就想到了Response.WriteFile(),但是,因為我的是模態窗口,我去掉了頁面緩存.這樣就沒辦法實現了.廢話不多說了,最后我的解決方法是利用頁面傳值方式(簡單吧,就怕沒想到),將文件路徑和文件名傳到另一個頁面,然后用Request.QueryString獲取值.
下面代碼分別是附件頁面和下載頁面.
string?slink?="<div><TABLE??border=0><TR><TD 附件名稱</TD><TD> 附件說明</TD></TR>";
for(int?j?=0;j<dt.Rows.Count;j++)
????????????{
????????????????string?strpath?=?Server.UrlEncode(GlobalVar.UploadPath+?"/"?+dt.Rows[j]["Filename"].ToString());
????????????????string?strfilename?=?Server.UrlEncode(dt.Rows[j]["Filename"].ToString());
????????????????string?strUrl?=?"FileDownLoad.aspx?pathname="+strpath?+?"&filename="+?strfilename;
????????????????slink?+=?"<TR><TD><a?href=\""+?strUrl?+"\"?target=\"_blank\")>"+?dt.Rows[j]["Filename"].ToString()?+"</a>    </TD><TD>"+dt.Rows[j]["Explain"].ToString()+"</TD></TR>";
????????????} 其中的dt 是附件表,GlobalVar.UploadPath是附件地址,Server.UrlEncode是為了中文附件名的傳值.
FileDownLoad.aspx的.cs文件如下(FileDownLoad沒有用BasePage的)
private?void?Page_Load(object?sender,?System.EventArgs?e)
????????{
????????????//?在此處放置用戶代碼以初始化頁面
????????????string?sfilePath?=?Request.QueryString["pathname"];
????????????string?sfilename?=?Request.QueryString["filename"];
????????????Response.Clear();
????????????Response.Charset?=?"utf-8";
????????????Response.Buffer=?true;
????????????this.EnableViewState?=?false;
????????????Response.ContentEncoding?=?System.Text.Encoding.UTF8;
????????????Response.AppendHeader("Content-Disposition","attachment;filename="?+?HttpUtility.UrlEncode(sfilename,?System.Text.Encoding.UTF8));?
????????????Response.WriteFile(sfilePath);?
????????????Response.Flush();
????????????Response.Close();
????????????Response.End();
????????}
????????????Response.Clear();
????????????Response.Buffer?=?true;
????????????Response.ExpiresAbsolute?=?System.DateTime.Now.AddSeconds(-1);
????????????Response.Expires?=?0;
????????????Response.CacheControl?=?"no-cache";
????????????string?nocache?=?"<meta?http-equiv=\"pragma\"?content=\"no-cache\">\n";
????????????nocache?+=?"<meta?http-equiv=\"cache-control\"?content=\"no-cache,?must-revalidate\">\n";
????????????nocache?+=?"<meta?http-equiv=\"expires\"?content=\"WED,?26?Feb?1997?08:21:57?GMT\">\n";
????????????Response.Write(nocache); ?可因為一個需求,需要在模態窗口中添加附件的下載功能,因為對附件個數的不確定,所以剛開始的解決方法是直接將附件路徑寫在<a href></a>里,單擊直接下載,使用過程中遇到了問題,譬如,當文件是圖片或者是TXT文件是,IE瀏覽器是直接在線打開的.而我要求是點擊后提示是否保存或者打開(注:模態窗口禁用了右鍵,所以沒有另存為).
??????怎樣才能讓IE出現提示呢?我就想到了Response.WriteFile(),但是,因為我的是模態窗口,我去掉了頁面緩存.這樣就沒辦法實現了.廢話不多說了,最后我的解決方法是利用頁面傳值方式(簡單吧,就怕沒想到),將文件路徑和文件名傳到另一個頁面,然后用Request.QueryString獲取值.
下面代碼分別是附件頁面和下載頁面.
string?slink?="<div><TABLE??border=0><TR><TD 附件名稱</TD><TD> 附件說明</TD></TR>";
for(int?j?=0;j<dt.Rows.Count;j++)
????????????{
????????????????string?strpath?=?Server.UrlEncode(GlobalVar.UploadPath+?"/"?+dt.Rows[j]["Filename"].ToString());
????????????????string?strfilename?=?Server.UrlEncode(dt.Rows[j]["Filename"].ToString());
????????????????string?strUrl?=?"FileDownLoad.aspx?pathname="+strpath?+?"&filename="+?strfilename;
????????????????slink?+=?"<TR><TD><a?href=\""+?strUrl?+"\"?target=\"_blank\")>"+?dt.Rows[j]["Filename"].ToString()?+"</a>    </TD><TD>"+dt.Rows[j]["Explain"].ToString()+"</TD></TR>";
????????????} 其中的dt 是附件表,GlobalVar.UploadPath是附件地址,Server.UrlEncode是為了中文附件名的傳值.
FileDownLoad.aspx的.cs文件如下(FileDownLoad沒有用BasePage的)
private?void?Page_Load(object?sender,?System.EventArgs?e)
????????{
????????????//?在此處放置用戶代碼以初始化頁面
????????????string?sfilePath?=?Request.QueryString["pathname"];
????????????string?sfilename?=?Request.QueryString["filename"];
????????????Response.Clear();
????????????Response.Charset?=?"utf-8";
????????????Response.Buffer=?true;
????????????this.EnableViewState?=?false;
????????????Response.ContentEncoding?=?System.Text.Encoding.UTF8;
????????????Response.AppendHeader("Content-Disposition","attachment;filename="?+?HttpUtility.UrlEncode(sfilename,?System.Text.Encoding.UTF8));?
????????????Response.WriteFile(sfilePath);?
????????????Response.Flush();
????????????Response.Close();
????????????Response.End();
????????}
轉載于:https://www.cnblogs.com/zjy/archive/2006/10/16/529966.html
總結
以上是生活随笔為你收集整理的用页传值方式解决模态窗口的Response.WriteFile文件下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP调用sql server 存储过程
- 下一篇: “没有强名称”解决