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

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

posted on 2006-09-30 23:37  bike  阅读(681)  评论(0编辑  收藏  举报

导航