codeigniter 无法同时下载两个不同的文件PHP

flvtvl50  于 2022-12-06  发布在  PHP
关注(0)|答案(1)|浏览(117)

我在PHP工作,我想知道这是可能的下载“两个不同的文件”在同一次点击?我尝试了以下代码

$pth    =   file_get_contents(base_url()."path/to/the/file.pdf");
$nme    =   "sample_file.pdf";
$pth2    =   file_get_contents(base_url()."path/to/the/file2.pdf");
$nme2    =   "sample_file2.pdf";
force_download($nme, $pth);
force_download($nme2, $pth2);
pvcm50d1

pvcm50d11#

不,在服务器端(不管是什么语言),你不能在同一个请求中触发两个不同的下载。你可以通过javascript来实现,如下所示:

function downloadFile(file) {
    // Create a link and set the URL using `createObjectURL`
    const link = document.createElement("a");
    link.href = URL.createObjectURL(file);
    // the link is invisible so do not show on the page
    link.style.display = "none";
    // this ensures the file is downloaded even if is a html one
    // you can set to true, or specify with which name the file will be downloaded 
    link.download = file.name;

    // attach to the DOM so it can be clicked
    document.body.appendChild(link);
    // click the link
    link.click();
    // this should free memory, usefull if you download many
    // files without page reload
    URL.revokeObjectURL(link.href);
    // remove the link from the DOM
    document.body.removeChild(link);
}

downloadFile('http://mywebsite/dowload/file_1.pdf');
downloadFile('http://mywebsite/dowload/file_2.docx');

相关问题