日期:2014-05-17 浏览次数:20396 次
public class DownHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//获取要下载的的文件名
string file = context.Request["downfile"];
if (System.IO.File.Exists(file))
{
FileStream fs = new FileStream(file, FileMode.Open);
byte[] fileBytes = new byte[(int)fs.Length];
fs.Read(fileBytes, 0, fileBytes.Length);
fs.Close();
context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(file, System.Text.Encoding.UTF8));
context.Response.AppendHeader("Content-Length", fileBytes.Length.ToString());
context.Response.ContentType = "application/octet-stream";
context.Response.OutputStream.Write(fileBytes, 0, fileBytes.Length);
context.Response.End();
}
}
}
function DownAtt(file){
//JS调用后台DownHandler下载文件,怎样将文件存储在本地?
$.get("DownHandler.ashx?id=3",{ downfile: file }, function (data) {
return true;
});
}