下载文件出现提示框或者直接显示在浏览器中

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

有个朋友问我下载弹出提示框的写法,具体如下:

出现提示框

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航