用页传值方式解决模态窗口的Response.WriteFile文件下载

    因为项目需要,使用了模态窗口,故在BasePage中的override void OnInit(EventArgs e)中加入如下内容.
None.gif             Response.Clear();
None.gif            Response.Buffer 
=   true ;
None.gif            Response.ExpiresAbsolute 
=  System.DateTime.Now.AddSeconds( - 1 );
None.gif            Response.Expires 
=   0 ;
None.gif            Response.CacheControl 
=   " no-cache " ;
None.gif            
string  nocache  =   " <meta http-equiv=\ " pragma\ "  content=\ " no - cache\ " >\n " ;
None.gif            nocache 
+=   " <meta http-equiv=\ " cache - control\ "  content=\ " no - cache, must - revalidate\ " >\n " ;
None.gif            nocache 
+=   " <meta http-equiv=\ " expires\ "  content=\ " WED,  26  Feb  1997   08 : 21 : 57  GMT\ " >\n " ;
None.gif            Response.Write(nocache);
 可因为一个需求,需要在模态窗口中添加附件的下载功能,因为对附件个数的不确定,所以刚开始的解决方法是直接将附件路径写在<a href></a>里,单击直接下载,使用过程中遇到了问题,譬如,当文件是图片或者是TXT文件是,IE浏览器是直接在线打开的.而我要求是点击后提示是否保存或者打开( 注:模态窗口禁用了右键,所以没有另存为).
      怎样才能让IE出现提示呢?我就想到了Response.WriteFile(),但是,因为我的是模态窗口,我去掉了页面缓存.这样就没办法实现了.废话不多说了,最后我的解决方法是利用页面传值方式(简单吧,就怕没想到),将文件路径和文件名传到另一个页面,然后用Request.QueryString获取值.
下面代码分别是附件页面和下载页面.
None.gif string  slink  = " <div><TABLE  border=0><TR><TD&nbsp附件名称</TD><TD>&nbsp附件说明</TD></TR> " ;
None.gif
for ( int  j  = 0 ;j < dt.Rows.Count;j ++ )
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                
string strpath = Server.UrlEncode(GlobalVar.UploadPath+ "/" +dt.Rows[j]["Filename"].ToString());
InBlock.gif                
string strfilename = Server.UrlEncode(dt.Rows[j]["Filename"].ToString());
InBlock.gif                
string strUrl = "FileDownLoad.aspx?pathname="+strpath + "&filename="+ strfilename;
InBlock.gif                slink 
+= "<TR><TD><a href=\""+ strUrl +"\" target=\"_blank\")>"+ dt.Rows[j]["Filename"].ToString() +"</a>&nbsp&nbsp&nbsp&nbsp</TD><TD>"+dt.Rows[j]["Explain"].ToString()+"</TD></TR>";
ExpandedBlockEnd.gif            }
其中的dt 是附件表,GlobalVar.UploadPath是附件地址,Server.UrlEncode是为了中文附件名的传值.
FileDownLoad.aspx的.cs文件如下(FileDownLoad没有用BasePage的)
None.gif private   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            string sfilePath = Request.QueryString["pathname"];
InBlock.gif            
string sfilename = Request.QueryString["filename"];
InBlock.gif            Response.Clear();
InBlock.gif            Response.Charset 
= "utf-8";
InBlock.gif            Response.Buffer
= true;
InBlock.gif            
this.EnableViewState = false;
InBlock.gif            Response.ContentEncoding 
= System.Text.Encoding.UTF8;
InBlock.gif            Response.AppendHeader(
"Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(sfilename, System.Text.Encoding.UTF8)); 
InBlock.gif            Response.WriteFile(sfilePath); 
InBlock.gif            Response.Flush();
InBlock.gif            Response.Close();
InBlock.gif            Response.End();
ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/zjy/archive/2006/10/16/529966.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值