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

前端 未结 9 1068
不知归路
不知归路 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:02

    This was the method which worked best for me and didn't open up new tabs, but just downloaded the files/images I required:

    var filesForDownload = [];
    filesForDownload( { path: "/path/file1.txt", name: "file1.txt" } );
    filesForDownload( { path: "/path/file2.jpg", name: "file2.jpg" } );
    filesForDownload( { path: "/path/file3.png", name: "file3.png" } );
    filesForDownload( { path: "/path/file4.txt", name: "file4.txt" } );
    
    $jq('input.downloadAll').click( function( e )
    {
        e.preventDefault();
    
        var temporaryDownloadLink = document.createElement("a");
        temporaryDownloadLink.style.display = 'none';
    
        document.body.appendChild( temporaryDownloadLink );
    
        for( var n = 0; n < filesForDownload.length; n++ )
        {
            var download = filesForDownload[n];
            temporaryDownloadLink.setAttribute( 'href', download.path );
            temporaryDownloadLink.setAttribute( 'download', download.name );
    
            temporaryDownloadLink.click();
        }
    
        document.body.removeChild( temporaryDownloadLink );
    } );
    

提交回复
热议问题