读书人

JS接到后台传输过来的文件后,如何把文

发布时间: 2013-06-25 23:45:41 作者: rapoo

JS接到后台传输过来的文件后,怎么把文件存储在本地啊?
DownHandler.ashx后台代码


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();
}
}
}


JS代码

function DownAtt(file){
//JS调用后—ownHandler下载文件,怎样将文件存储在本地?
$.get("DownHandler.ashx?id=3",{ downfile: file }, function (data) {

return true;
});
}

[解决办法]
直接用window.location="downHandler.ashx?.."就可以下载了

读书人网 >asp.net

热点推荐