110,550
社区成员
发帖
与我相关
我的任务
分享
protected override void PageLoad(object sender, EventArgs e)//初始化方法
{
string sfilePath = Request["path"];
string sfilename = System.IO.Path.GetFileName(sfilePath);
Download(Response, sfilePath, sfilename);
}
public void Download(HttpResponse Response, string downloadFile, string downloadFileName)
{
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(downloadFileName));
Response.Charset = "Shift_JIS";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("shift-jis");
Response.CacheControl = "Private";
byte[] buff = new byte[1024];
using (FileStream fs = File.OpenRead(downloadFile))
{
int len;
while ((len = fs.Read(buff, 0, buff.Length)) > 0)
{
if (len != buff.Length)
{
byte[] buff2 = new byte[len];
Array.Copy(buff, buff2, len);
Response.BinaryWrite(buff2);
}
else
{
Response.BinaryWrite(buff);
}
}
}
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
public override void ProcessRequest(HttpContext context)
{
string sfilePath = context.Request["path"];
string sfilename = System.IO.Path.GetFileName(sfilePath);
Download(context.Response, sfilePath, sfilename);
}