下载文件

///


/// 下载pdf
///

/// pdf路径
private void DownloadFile(string filepath)
{
if (File.Exists(filepath))
{
using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);

if (buffer != null && buffer.Length > 0)
{
this.Response.Clear();
this.Response.Buffer = true;
this.Response.Charset = "UTF-8"; //GB2312/UTF-8

this.Response.AppendHeader("Content-Disposition", "attachment;filename="
+ HttpUtility.UrlEncode("document.pdf", System.Text.Encoding.UTF8));

this.Response.ContentType = "application/octet-stream";
this.Response.BinaryWrite(buffer);
this.Response.Flush();
this.Response.End();
this.Response.Close();
}
}
}
}

你可能感兴趣的:(下载文件)