怎样下载附件
各位大哥,小弟前几天把附件上传的数据库里面了,但是上传的附件我只是把它的名字上传到数据库里面了,也就是说数据库里面只存名字了,那么我用Datagrid把它显示出来,我在每一行的后面添加一个按扭列,起名叫下载附件,那么问题来了,我怎么点击下载附件的时候能保存附件或者下载呀,
------解决方案--------------------WebClient myclient=new WebClient();
string strFile= "默认路径+数据库里的文件名称参数 "; //服务器上要共下载的文件
Response.Clear();
Response.ClearHeaders();
Response.Charset = "GB2312 ";
Response.ContentEncoding =System.Text.Encoding.UTF8;
Response.ContentType = "application/octet-stream ";
Response.AddHeader( "Content-Disposition ",
"attachment;filename= " +
System.Web.HttpUtility.UrlEncode(strFile)) ;
Response.WriteFile(strFile);
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= " + System.Web.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();
}