我正在开发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);发布于 2022-10-06 08:35:07
不,在服务器端(与语言无关),您不能在同一个请求中触发两个不同的下载。您可以通过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');https://stackoverflow.com/questions/73970146
复制相似问题