不使用Zip文件下载多个文件

我有一个通用处理程序Document.ashx ,它通过读取查询字符串中的信息来动态创建Word文档,如Document.ashx?clientid=123&documentid=10 ,它完美地运行。

我需要创建一个带有复选框列表和“ Download All按钮的界面。 到目前为止我最好的想法是使用这样的东西来调用处理程序。

 $("body").append(" ") 

Chrome和Firefox按预期处理此问题,但IE9会提示用户询问是否要保存第一个文件但忽略以下文件。

如何从客户端开始下载多个文件?

这是一个内部网站点,因此文件总是在〜1秒内生成,用户一次选择〜3-5个文档。 绝大多数用户都在使用IE9。 我可以告诉每个人他们必须使用Firefox或Chrome,但我宁愿找到适用于所有现代浏览器的解决方案。

我不想创建一个zip文件服务器端,因为它们总是必须首先解压缩它(这对某些人来说太难理解)并且它会减慢它们的速度。

所以这可能是矫枉过正,但适用于IE9,FF7和Chrome 16:

灵感来自这篇SOpost

jQuery插件:

  • json2.js
  • $ .cookie()
  • $ .bbq()

处理程序中的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(""); } 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); }); 

jQuery插件,它将为您完成工作:

github.com/biesiad/multiDownload

这可能会或可能不会解决您的问题,但您在此处犯了一个常见错误。 iframe不是自动关闭标签。 许多浏览器无法正确解析此html。 尝试制作它

 $("body").append(" ") 

此外,您希望尝试单独附加每个iframe,因为浏览器可能无法在一次添加所有帧时正确识别帧:

 $("body").append(""); $("body").append(""); 

我知道这是一个老问题,但我最近遇到了这个问题并创建了最适合我需求的解决方案(我不想使用任何cookie并希望代码尽可能简单)。 代码背后:

 Protected Sub DownloadFile(fileId As String) If Request.Browser.Browser = "Chrome" Then 'open iframes dynamically for multiple downloads ClientScript.RegisterStartupScript(Me.GetType(), fileId, _ "") Else 'open windows for multiple downloads ClientScript.RegisterStartupScript(Me.GetType(), fileId, _ "") 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上一直在为我工作。

您可以在这样的javascript循环中调用下载:

 Download All