How can I let a user download multiple files when a button is clicked?

前端 未结 9 1005
不知归路
不知归路 2020-11-27 18:18

So I have a httpd server running which has links to a bunch of files. Lets say the user selects three files from a file list to download and they\'re located at:

         


        
相关标签:
9条回答
  • 2020-11-27 19:04

    This works in all browsers (IE11, Firefox, Microsoft Edge, Chrome and Chrome Mobile) My documents are in multiple select elements. The browsers seem to have issues when you try to do it too fast... So I used a timeout.

    <select class="document">
        <option val="word.docx">some word document</option>
    </select>
    
    //user clicks a download button to download all selected documents
        $('#downloadDocumentsButton').click(function () {
            var interval = 1000;
            //select elements have class name of "document"
            $('.document').each(function (index, element) {
                var doc = $(element).val();
                if (doc) {
                    setTimeout(function () {
                        window.location = doc;
                    }, interval * (index + 1));
                }
            });
        });
    

    This solution uses promises:

    function downloadDocs(docs) {
        docs[0].then(function (result) {
            if (result.web) {
                window.open(result.doc);
            }
            else {
                window.location = result.doc;
            }
            if (docs.length > 1) {
                setTimeout(function () { return downloadDocs(docs.slice(1)); }, 2000);
            }
        });
    }
    
     $('#downloadDocumentsButton').click(function () {
        var files = [];
        $('.document').each(function (index, element) {
            var doc = $(element).val();
            var ext = doc.split('.')[doc.split('.').length - 1];
    
            if (doc && $.inArray(ext, docTypes) > -1) {
                files.unshift(Promise.resolve({ doc: doc, web: false }));
            }
            else if (doc && ($.inArray(ext, webTypes) > -1 || ext.includes('?'))) {
                files.push(Promise.resolve({ doc: doc, web: true }));
            }
        });
    
        downloadDocs(files);
    });
    
    0 讨论(0)
  • 2020-11-27 19:08

    The best way to do this is to have your files zipped and link to that:

    The other solution can be found here: How to make a link open multiple pages when clicked

    Which states the following:

    HTML:

    <a href="#" class="yourlink">Download</a>
    

    JS:

    $('a.yourlink').click(function(e) {
        e.preventDefault();
        window.open('mysite.com/file1');
        window.open('mysite.com/file2');
        window.open('mysite.com/file3');
    });
    

    Having said this, I would still go with zipping the file, as this implementation requires JavaScript and can also sometimes be blocked as popups.

    0 讨论(0)
  • 2020-11-27 19:12

    You can either:

    1. Zip the selected files and return the one zipped file.
    2. Open multiple pop-ups each prompting for a download.

    Note - option one is objectively better.

    Edit Found an option three: https://stackoverflow.com/a/9425731/1803682

    0 讨论(0)
提交回复
热议问题