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