下载文件出现提示框或者直接显示在浏览器中
有个朋友问我下载弹出提示框的写法,具体如下:
出现提示框
string strfile="f:\\a.doc";//路径根据实际情况而定
if(!system.io.file.exists(strfile))
{
response.write("<script language='javascript'>alert('对不起,文件不存在!');</script>");
return;
}
response.clear();
response.clearheaders();
response.charset = "gb2312";
response.contentencoding =system.text.encoding.utf8;
response.contenttype = "application/octet-stream";
fileinfo fi=new fileinfo(strfile);
response.addheader("content-disposition","attachment; filename=" + httputility.urlencode(fi.name)) ;
response.addheader("content-length",fi.length.tostring());
byte[] tmpbyte=new byte[1024*8];
filestream fs=fi.openread();
int count;
while((count=fs.read(tmpbyte,0,tmpbyte.length))>0)
{
response.binarywrite(tmpbyte);
response.flush();
}
fs.close();
response.end();
直接在浏览器中打开
string strfile="f:\\a.doc";//路径根据实际情况而定
response.clear();
response.clearheaders();
response.charset = "gb2312";
response.contentencoding =system.text.encoding.utf8;
response.contenttype = "application/msword";
response.writefile(strfile);
http://www.cnblogs.com/skylaugh/archive/2006/12/18/596074.html