您现在的位置: 网页制作教程网 >> 网络编程 >> Asp.net 教程 >> ASP.NET 开发技巧 >> 文章正文

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

作者:AspCool

来源:www.ASPCool.com

热度:

2006-10-14 9:32:07

 原来我们项目在开发时中(文件是存储在数据库中)下载文件采用写入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();
   }

我来说两句:

1分 2分 3分 4分 5分
姓名: *


* 请各位网友遵纪守法并注意语言文明。
网站简介 | 联系方式 | 意见建议 | 版权说明
Copyright © 2007 All rights reserved
滇ICP备06006992号