简体   繁体   English

使用 Puppeteer 下载文件时 Chrome 下载错误

[英]Chrome download error when downloading file with Puppeteer

I have an application that shows a page, the user clicks on a button, and downloads a CSV file.我有一个显示页面的应用程序,用户单击一个按钮,然后下载 CSV 文件。 I want to run this with Puppeteer.我想用 Puppeteer 运行它。

Problem is that the CSV is downloaded empty and with an error.问题是 CSV 下载为空并出现错误。 This happens both with headless true and false. headless真假都会发生这种情况。 The page finished loading, and I increased the timeout, but it still fails.页面加载完毕,我增加了超时,但仍然失败。 What could be the issue?可能是什么问题?

在此处输入图像描述

const puppeteer = require('puppeteer');

(async () => {
    
    const browser = await puppeteer.launch({
       headless: false
    });

    const page = await browser.newPage();
    await page.goto('http://localhost:4400/login', { waitUntil: 'networkidle2' });    
    
    await page._client.send('Page.setDownloadBehavior', {
        behavior: 'allow',
        downloadPath: './',
    });

    await page.waitForSelector('#run-and-export');
    await page.click('#run-and-export');
  
    // file-downloaded is turned on when the file finished downloading (not to close the window)
    await page.waitForSelector('#file-downloaded', { timeout: 120000 }); 

    await browser.close();
    
})();

The code in the application that generates the file to download is an Angular service:应用程序中生成要下载的文件的代码是一个 Angular 服务:

@Injectable({ 
    providedIn: 'root' 
})
export class DownloadService {

    downloadFile(content:any, fileName: string, mimeType: string){
    
        var blob = new Blob([(content)], {type: mimeType});
        var a = document.createElement('a');
        a.href = window.URL.createObjectURL(blob);
        a.download = fileName;
        a.click();

    }
}

This is what made this work:这就是使这项工作发挥作用的原因:

const downloadPath = path.resolve('/my/path');

await page._client.send('Page.setDownloadBehavior', {
    behavior: 'allow',
    downloadPath: downloadPath 
});

I got the same problem, the download failed, in the download dir I got filename.pdf.crdownload and no other file.我遇到了同样的问题,下载失败,在下载目录中我得到了filename.pdf.crdownload而没有其他文件。

The download dir is up two levels from the app dir ../../download_dir下载目录比应用程序目录../../download_dir

The solution was (as suggested by ps0604):解决方案是(如 ps0604 建议的那样):

const path = require('path');
const download_path = path.resolve('../../download_dir/');

await page._client.send('Page.setDownloadBehavior', {
    behavior: 'allow',
    userDataDir: './',
    downloadPath: download_path,
});

If someone is searching for .crdownload file and download error.如果有人在搜索.crdownload文件并下载错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .lnk文件在Chrome中以.download的形式下载,但在Mozilla的第一次尝试中无法下载 - .lnk File downloading as .download in Chrome and it failed to download in first attempt in Mozilla 为什么当我在启用“下载前询问每个文件的保存位置”选项的情况下调用 chrome.downloads.download 方法时下载会中断 - Why is the download interrupted when i call chrome.downloads.download method with enabled "Ask where to save each file before downloading" option 创建多行文件并使用Chrome.downloads.download下载 - Creating a multi-line file and downloading with Chrome.downloads.download 通过iframe下载文件时出现Chrome错误 - Chrome error on downloading file via iframe 使用 Express、Axios 和 Js-File-Download 下载文件时出错 - File Downloading Error Using Express, Axios and Js-File-Download Puppeteer 不会打开 chrome(出现错误) - Puppeteer will not open chrome (getting error) 从 MongoDB 下载文件时在 Chrome 中出现“失败 - 文件不完整” - Getting "Failed - File incomplete" in Chrome when downloading file from MongoDB 使用node-webkit下载文件时出错(URL中的“ chrome-extension”) - Error on downloading file with node-webkit ( “chrome-extension” in the URL) 下载 pdf 文件 (blob) 时出现 javascript 错误 - javascript error when downloading pdf file (blob) 如何通过直接链接 Puppeteer 下载文件? - How to download file with direct link Puppeteer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM