首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法同时下载两个不同的文件

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

Stack Overflow用户
提问于 2022-10-06 07:39:29
回答 1查看 52关注 0票数 0

我正在开发Php,我想知道这是否可以在相同的点击下下载“两个不同的文件”?我尝试使用以下代码

代码语言:javascript
复制
$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);
EN

回答 1

Stack Overflow用户

发布于 2022-10-06 08:35:07

不,在服务器端(与语言无关),您不能在同一个请求中触发两个不同的下载。您可以通过javascript这样做,如下所示:

代码语言: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');
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73970146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档