打印

[asp.net教程] 解决文件下载弹出窗体被拦截

解决文件下载弹出窗体被拦截

原来我们项目在开发时中(文件是存储在数据库中)下载文件采用写入http头的形式。如下 response.clear();
  response.buffer = false;
  response.appendheader("content-disposition","attachment;filename="+httputility.urlencode(filewjnr.rows[0]["wjm"].tostring(),system.text.encoding.utf8));
  response.binarywrite(字节流);
  response.end();
   
   但在项目部署后,用户的ie6.0浏览时,会被拦截,并关闭退出。当时同事用了弹出一个窗体,再在弹出的窗体中再“点击下载”,这样就不会被拦截。
   我试了一个更直接的解决方法,就是点击时,先生成临时文件,再链接至临时文件,即弹出文件下载或打开对话框。代码很简单:
   
   string filename = "文件名" //用文件id
   string tempfilepath = request.physicalpath;
   tempfilepath = tempfilepath.substring(0,tempfilepath.lastindexof("\\"));
   tempfilepath += "\\temp\\" + filename;
   filestream file = new filestream(tempfilepath,filemode.openorcreate,fileaccess.readwrite);
   
   try
   {
   
   byte[] docbody = (byte[])filewjnr.rows[0]["wjnr"]; //转换
   file.write(docbody, 0, docbody.length);
   file.close();
   response.redirect("temp\\" + filename);
   }
   catch
   {
   file.close();

   }

TOP

返回顶部
AYBlue

Processed in 0.040641 second(s), 7 queries.

当前时区 GMT+8, 现在时间是 2009-1-10 11:09 京ICP备06054220号

清除 Cookies - 联系我们 - 163K.com - Archiver - WAP