【问题标题】:Multiple PDFdownloads多个 PDF 下载
【发布时间】:2019-07-20 18:09:06
【问题描述】:

我有一个本地数据库表,其中保存了大约 15000 个 PDF 链接。我想一键下载所有 PDF,但我的问题是它的打开 PDF 没有下载。我正在尝试这种方法。

items = Array.from(document.getElementsByTagName("a"));
items.forEach(function(item) {
    link = item.href;
    if (link.substr(link.length - 4) == ".pdf") {
        filename = link.replace(/^.*[\\\/]/, '');
        item.download = filename;
        item.click();
    }
});

【问题讨论】:

标签: javascript


【解决方案1】:

您不能仅使用 1 次单击下载所有文件。你可以在 PHP 中使用 ZIP 归档类来代替。

将所有可用的 pdf 制作成一个 zip 文件并下载。

$files = array('pdf1.pdf','pdf2.pdf');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);

  foreach ($files as $file) {
    $zip->addFile($file);
  }

  $zip->close();

标题喜欢

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);

【讨论】:

  • 它会下载一个 zip 文件,但在解压过程中显示未找到存档。 $files = array('assets.supply.com/ul_pdfs/309006_SpecSheet.pdf','https:/…); $zipname = '文件.zip'; $zip = 新的 ZipArchive; $zip->open($zipname, ZipArchive::CREATE); foreach ($files as $file) { $zip->addFile($file); } $zip->close(); header('Content-Type: application/zip'); header('内容配置:附件;文件名='.$zipname); header('内容长度:' .filesize($zipname));读取文件($zipname);我错过了什么吗?
【解决方案2】:

感谢您的回复, 此代码适用于我的 zip 存档

$files = array('pdflink','pdflink');
$zip = new ZipArchive();
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);

foreach($files as $file){
    $download_file = file_get_contents($file);
    $zip->addFromString(basename($file),$download_file);
}   
$zip->close();
header('Content-disposition: attachment; filename=file.zip');
header('Content-type: application/zip');
readfile($tmp_file);

?>

【讨论】:

    猜你喜欢
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 2013-07-10
    • 2021-09-24
    • 1970-01-01
    相关资源
    最近更新 更多