private void DownLoadFile(string FileName)
{
if(!System.IO.File.Exists(FileName))
{
Response.Write("<script language='javascript'>alert('对不起,文件不存在!');</script>");
return;
}
Response.Clear();
Response.ClearHeaders();
Response.BufferOutput=false;
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.ContentType="application/octet-stream";
FileInfo fi=new FileInfo(FileName);
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();
}该函数第一次执行时没有问题,能够下载成功,但当第二次执行时整个页面变成了刚才下载的文件内容的一部分,这是怎么回事?请高手解决.
{
if(!System.IO.File.Exists(FileName))
{
Response.Write("<script language='javascript'>alert('对不起,文件不存在!');</script>");
return;
}
Response.Clear();
Response.ClearHeaders();
Response.BufferOutput=false;
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.ContentType="application/octet-stream";
FileInfo fi=new FileInfo(FileName);
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 sql = System.Configuration.ConfigurationManager.AppSettings["strConn"].ToString();
SqlConnection strConn = new SqlConnection(sql);
strConn.Open();
string sql1 = " select * from 公告表 where id = '"+newsid +"'";
SqlCommand mycommand = new SqlCommand(sql1, strConn);
SqlDataReader dr = mycommand.ExecuteReader();
if (dr.Read())
{
//string km = dr["附件类型"].ToString().Trim();
Response.ContentType = "application/x-msdownload";
string fn = dr["文件名"].ToString().Trim ();
string name = fn ;
// 添加 信息, "文件下 /另存 " 框指定默 文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(name ));
// 添加 信息,指定文件大小, 浏览器显示下载进度
//Response.AddHeader("Content-Length", barrImage.Length.ToString());
//Response.BinaryWrite(ms.ToArray());
Response.BinaryWrite((byte[])dr["附件"]);
Response.Flush();
Response.End();
}
dr.Close();
strConn.Close();
}
System.IO.FileInfo file = new System.IO.FileInfo(FileName);
if (file.Exists)
{
Response.Clear();
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Filter.Close();
Response.WriteFile(file.FullName);
Response.End();
}
if(System.IO.File.Exists(fullPath) )
{
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode("文件名"));
Response.WriteFile(fullPath);
Response.End();
}
Response.WriteFile(fullPath);
Response.End();
没有下载过程,
用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();
}
可以显示下载过程!