【发布时间】:2011-11-09 17:15:07
【问题描述】:
我正在尝试使用 xmlhttprequest 实现文件下载...但没有得到文件对话框或任何响应。我已经调试处理程序,没有错误。
function download(id)
{
try
{
var xmlHttp=new XMLHttpRequest();
xmlHttp.open("GET","DownloadFileHandler.ashx?id=" + id,false);
xmlHttp.send();
xmlHttp.onreadystatechange=function()
{
//if request has been entertained and response is returned from server
if(xmlHttp.readyState==4)
{
alert("aq");
}
}
}
catch (ex)
{
alert("Browser does not support ajax");
}
}
}
我的处理程序
context.Response.AppendHeader("content-disposition", "attachment; filename=" + name);
context.Response.ContentType = type;
context.Response.WriteFile(path);
context.Response.End();
【问题讨论】:
-
不能异步工作...
-
文件下载是一个二进制 http 请求。 XmlHttpRequest 支持 AJAX。它们不兼容。由于必须启动点击,因此最好的办法是让 url 指向一个 .ashx 处理程序,该处理程序可以检索文件并在外部执行传递。