我有一个通用处理程序Document.ashx
,可以通过读取查询字符串中的信息来动态创建Word文档,就像这个Document.ashx?clientid=123&documentid=10
一样,它可以很好地工作。
我需要创建一个带有复选框列表和Download All
按钮的界面。我到目前为止最好的想法是使用这样的东西来调用处理程序。
$("body").append("<iframe src='Document.ashx?clientid=123&documentid=10'></iframe>
<iframe src='Document.ashx?clientid=123&documentid=11'></iframe>")
Chrome和Firefox会按预期处理此问题,但IE9会提示用户询问是否要保存第一个文件但忽略以下文件。
如何从客户端开始下载多个文件?
这是一个内部网站点,所以文件总是在〜1秒内生成,用户一次选择〜3-5个文档。绝大多数用户都在使用IE9。我可以告诉每个人他们必须使用Firefox或Chrome,但我宁愿找到一个适用于所有现代浏览器的解决方案。
我不想创建一个zip文件服务器端,因为它们总是必须首先解压缩它(这对某些人来说太难理解)并且会减慢它们的速度。
答案 0 :(得分:10)
所以这可能是矫枉过正,但适用于IE9,FF7和Chrome 16:
受this SO post启发
jQuery插件:
处理程序中的C#:
public void ProcessRequest (HttpContext context) {
...
if (!string.IsNullOrEmpty(context.Request.QueryString["downloadid"]))
Response.Cookies[context.Request.QueryString["downloadid"]].Value = "complete";
}
的Javascript / jQuery的:
function downloadFile(url, downloadid) {
//set a cookie with a unique download id
$.cookie(downloadid, 'pending', { path: '/' });
//create a new url
var newurl = $.param.querystring(url, { downloadid: downloadid });
//append an iframe with new url
$("body").append("<iframe style='height:0;width:0;' data-downloadid='" + downloadid + "' src='" + newurl + "'></iframe>");
}
function downloadComplete(downloadid) {
//check if download is pending
return $.cookie(downloadid) == "complete";
}
function downloadManager(arrDownloads) {
//loop through download items backwards
var allComplete = false;
for (var i = arrDownloads.length; i > 0; i--) {
if (downloadComplete(arrDownloads[i - 1].downloadid)) {
//download the next one if it exists
if (i == arrDownloads.length) {
allComplete = true;
}
else {
downloadFile(arrDownloads[i].url, arrDownloads[i].downloadid);
}
//stop checking for completed downloads
break;
}
}
if (allComplete) {
//remove cookies
for (var i = arrDownloads.length; i > 0; i--) {
$.cookie(arrDownloads[i - 1].downloadid, null, { path: '/' });
}
//remove iframes
$("iframe[data-downloadid]").remove();
}
else {
setTimeout("downloadManager(" + JSON.stringify(arrDownloads) + ");", 500);
}
}
function downloadFiles(arrurls) {
var arrDownloads = [];
for (var i = 0; i < arrurls.length; i++) {
var item = new Object();
item.url = arrurls[i];
item.downloadid = newGuid();
arrDownloads.push(item);
}
//start the first download
downloadFile(arrDownloads[0].url, arrDownloads[0].downloadid);
//initiate the manager
downloadManager(arrDownloads);
}
$(function () {
var arrurls = [];
arrurls.push("Document.ashx?clientid=123&documentid=10");
arrurls.push("Document.ashx?clientid=123&documentid=11");
arrurls.push("Document.ashx?clientid=123&documentid=12");
arrurls.push("Document.ashx?clientid=123&documentid=13");
arrurls.push("Document.ashx?clientid=123&documentid=14");
downloadFiles(arrurls);
});
答案 1 :(得分:5)
jQuery插件将为您完成工作:
答案 2 :(得分:0)
这可能会或可能不会解决您的问题,但您在此处犯了一个常见错误。 iframe不是自动关闭标签。许多浏览器无法正确解析此html。尝试制作
$("body").append("<iframe src='Document.ashx?clientid=123&documentid=10'>If you can read this, please use a newer browser</iframe>
<iframe src='Document.ashx?clientid=123&documentid=11'>If you can read this, please use a newer browser</iframe>")
此外,您希望尝试单独附加每个iframe,因为浏览器在一次添加时可能无法正确识别帧:
$("body").append("<iframe src='Document.ashx?clientid=123&documentid=10'>If you can read this, please use a newer browser</iframe>");
$("body").append("<iframe src='Document.ashx?clientid=123&documentid=10'>If you can read this, please use a newer browser</iframe>");
答案 3 :(得分:0)
我知道这是一个老问题,但我最近遇到了这个问题并创建了最适合我需求的解决方案(我不想使用任何cookie并希望代码尽可能简单)。代码背后:
Protected Sub DownloadFile(fileId As String)
If Request.Browser.Browser = "Chrome" Then
'open iframes dynamically for multiple downloads
ClientScript.RegisterStartupScript(Me.GetType(), fileId, _
"<script language='javascript'>createIframeForDownloadHandler('" & fileId & "');</script>")
Else
'open windows for multiple downloads
ClientScript.RegisterStartupScript(Me.GetType(), fileId, _
"<script language='javascript'>openWindowForDownloadHandler('" & fileId & "');</script>")
End If
End Sub
以下是javascript函数:
function openWindowForDownloadHandler(fileId) {
//open a new window. setting height and width foces new window as opposed to new tab
window.open('FileShareDownloadHandler.ashx?id=' + fileId, '_blank', 'width=100,height=100,left=0,top=0');
}
function createIframeForDownloadHandler(fileId) {
var element = document.createElement("iframe");
element.setAttribute('id', 'myframe' + fileId);
element.setAttribute('style', 'display:none;');
element.setAttribute('src', 'FileShareDownloadHandler.ashx?id=' + fileId);
document.body.appendChild(element);
}
将DownloadFile(id)放入循环中效果很好。
基本上,我发现chrome可以很好地处理iFrames,但是IE没有(我使用的是IE9)。这在Crome v26,FF v19,IE9上一直在为我工作。
答案 4 :(得分:0)
你可以打电话来下载这样的javascript循环:
<a id="downloadAll" href="#">Download All</a>
<script>
var downloadSelected = $('a#downloadSelected');
var doc_ids = ['10', '11'];
for (var i in doc_ids) {
var uri = 'Document.ashx?clientid=123&documentid=' + doc_ids[i];
var iframe = $("<iframe/>").attr({
src: uri,
style: "visibility:hidden;display:none"
}).appendTo(downloadAll);
}
</script>