I am download a file by going through .aspx page and which returns a file
我通过浏览.aspx页面下载文件并返回一个文件
HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
String Header = "Attachment; Filename=" + sFileName;
HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
FileInfo Dfile = new FileInfo(HttpContext.Current.Server.MapPath(sFilePath));
HttpContext.Current.Response.WriteFile(Dfile.FullName);
HttpContext.Current.Response.End();
and that's fine.
那没关系。
I want to be able to do this via async ajax call using jQuery so that while the file is being downloaded user sees a gif spinner animation.
我希望能够通过使用jQuery的异步ajax调用来做到这一点,这样在下载文件时,用户会看到一个gif微调器动画。
$("#showbusy").fadeIn();
$.ajax({ async : true, type: "GET", url: "download.aspx",
contentType: "application/text; charset=utf-8",
success: function (data) {
$("#showbusy").hide();
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
$("#showbusy").hide();
}
});
If I go directly to the .aspx page the file downloads, but this is not working by doing an ajax call to this page for some reason. I can see data is being returned in Firebug but once it completes the download it just sits there in memory.
如果我直接转到.aspx页面下载文件,但由于某种原因,这是通过对此页面执行ajax调用而无法正常工作的。我可以看到Firebug中正在返回数据,但是一旦完成下载,就会将其放在内存中。
How do I actually trigger a save file dialog on the browser side after the file download data has been received?
在收到文件下载数据后,如何在浏览器端实际触发保存文件对话框?
1 个解决方案
#1
0
The download will have to be initiated through the save dialog in order for the user to be able to put it on their disk.
必须通过保存对话框启动下载,以便用户能够将其放在磁盘上。
If your approach is to pre-fetch the file and then show the user a save dialog to put the file somewhere on their disk, this is not doable through javascript.
如果你的方法是预先获取文件,然后向用户显示一个保存对话框,将文件放在磁盘上的某个位置,这是不能通过javascript实现的。
However, you may be able to make it seem instantaneous for the user by doing the call to download the file through ajax (what you're doing now), provide response cache headers when sending the contents, and then popup the download dialog (through an iframe for example) for the exact same URL as the one in the AJAX call. This will get the browser to save the file from its cache (created when the AJAX call was done) to the user's disk.
但是,您可以通过调用通过ajax(现在正在执行的操作)下载文件,在发送内容时提供响应缓存标头,然后弹出下载对话框(通过例如,iframe)与AJAX调用中的URL完全相同。这将使浏览器将文件从其缓存(在完成AJAX调用时创建)保存到用户的磁盘。
#1
0
The download will have to be initiated through the save dialog in order for the user to be able to put it on their disk.
必须通过保存对话框启动下载,以便用户能够将其放在磁盘上。
If your approach is to pre-fetch the file and then show the user a save dialog to put the file somewhere on their disk, this is not doable through javascript.
如果你的方法是预先获取文件,然后向用户显示一个保存对话框,将文件放在磁盘上的某个位置,这是不能通过javascript实现的。
However, you may be able to make it seem instantaneous for the user by doing the call to download the file through ajax (what you're doing now), provide response cache headers when sending the contents, and then popup the download dialog (through an iframe for example) for the exact same URL as the one in the AJAX call. This will get the browser to save the file from its cache (created when the AJAX call was done) to the user's disk.
但是,您可以通过调用通过ajax(现在正在执行的操作)下载文件,在发送内容时提供响应缓存标头,然后弹出下载对话框(通过例如,iframe)与AJAX调用中的URL完全相同。这将使浏览器将文件从其缓存(在完成AJAX调用时创建)保存到用户的磁盘。