我有一个通用处理程序,我使用这个非常基本的 js 代码调用它:
var formData = new FormData();
formData.append("fileId", this.model.get("id"));
xhr = new XMLHttpRequest();
xhr.open('POST', '/genericHandlers/DownloadFile.ashx');
xhr.onload = function () {
if (xhr.status === 200) {
// Do something here?
}
}
xhr.send(formData);
我的通用处理程序代码对我来说似乎不错。基本上我正在尝试构建响应头,我认为当这个处理程序返回时,它会开始下载文件。
处理程序代码:
var fileId = context.Request.Form["fileId"];
// File stored in the db as a byte array
var file = (from f in dataContext.OneEVA_Docs_File_Storages
where Equals(f.ID, fileId)
select f).FirstOrDefault();
context.Response.Clear();
context.Response.AddHeader("Content-Type", file.ContentType);
context.Response.AddHeader("Content-Length", file.ContentLength.ToString());
context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}; size={1}", file.Name, file.ContentLength));
context.Response.BinaryWrite(file.File_Image.ToArray());
context.Response.Flush();
context.Response.End();
XHR
请求正常完成。这是我返回的响应标头:
Cache-Control:private
Connection:Close
Content-Disposition:attachment; filename=Mikes File; size=1860113
Content-Length:1860113
Content-Type:image/jpeg
Date:Wed, 16 May 2012 14:08:01 GMT
Server:ASP.NET Development Server/10.0.0.0
X-AspNet-Version:4.0.30319
我错过了什么?我究竟做错了什么?