下载指定文件
//DownloadFile(Page.Response, "D:\\a\\Brains.doc");
public void DownloadFile(HttpResponse response, string serverPath)
{
FileStream fs = null;
try
{
fs = File.OpenRead(serverPath);
byte[] buffer = new byte[1024];
long count = 1024;
response.Buffer = true;
response.AddHeader("Connection", "Keep-Alive");
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Disposition", "attachment;filename=" + Path.GetFileName(serverPath));//下载时要保存的默认文件名
response.AddHeader("Content-Length", fs.Length.ToString());
while (count == 1024)
{
count = fs.Read(buffer, 0, 1024);
response.BinaryWrite(buffer);
}
}
catch
{
}
finally
{
fs.Close();
}
}