jquery
  • asp.net
  • iframe
  • internet-explorer-9
  • asp.net-4.0
  • 2012-01-28 63 views 11 likes 
    11

    我有一个通用处理程序Document.ashx,通过从查询字符串中读取信息,像这样Document.ashx?clientid=123&documentid=10,它可以很好地创建Word文档。不使用Zip文件下载多个文件

    我需要创建一个界面,其中包含复选框和一个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

    我会尝试像[这](http://stackoverflow.com/a/4168965/585552) – Greg 2012-01-28 21:30:12

    回答

    10

    所以这是propably矫枉过正,但工作在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); 
    }); 
    
    +0

    这取决于第一次调用的成功。如果它失败了怎么办,然后再打电话给downloadFile? – 2017-02-09 02:58:59

    0

    这可能会也可能不会解决您的问题,但您在此处发生常见错误。 Iframes不是自闭标签。许多浏览器都不会正确解析这个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

    使用2 '.append()'语句效果更好(Chrome没有提示下载多个文件的警告),但它在IE9中仍然不起作用(它只识别第一个文件) – Greg 2012-01-28 21:12:23

    5

    的jQuery插件,它会做的工作适合你:

    github.com/biesiad/multiDownload

    +0

    Chrome仍然会给你一个黄色的条,说:“这个网站正在尝试下载多个文件,你想允许这个吗?”在下载第一个文件之后。 http://dl.dropbox.com/u/3115379/screengrab_20120719101330.png – Greg 2012-07-19 14:18:22

    +0

    那么..你正试图下载多个文件。黄色条是正确的。如果你想避免它,你可以在下载之间添加更多的延迟。查看“延迟”选项。 – biesiad 2012-07-25 12:19:59

    +0

    我已经把图片,而不是tar.zip在你的github测试,并没有工作铬最新版本 – shareef 2016-05-06 13:34:36

    0

    我知道这是一个老问题,但最近我有过这样的问题,并创造了这个解决方案最适合我的需要(我不想使用任何cookies并希望代码尽可能简单)。在后面的代码:

    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)内的循环效果很好。

    基本上,我发现铬适用于处理iFrames,但IE浏览器不(我使用IE9)。这在Crome v26,FF v19,IE9上为我工作。

    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> 
    
    相关问题