如何使用 puppeteer 将图像的 BUFFER 上传到任何网站?

问题描述 投票:0回答:1

这是我知道该怎么做:

但是,该解决方案仅适用于我的本地计算机。我希望在云中运行 puppeteer,这意味着我无法访问具有文件路径的静态磁盘。因此,我想做的是省略磁盘作为中间人。一旦我将图像作为缓冲区,我想直接将该缓冲区上传到输入中。我认为 elementHandle.uploadFile(path) 的源代码将是一个很好的起点,因为我几乎只是想剥离一层抽象。但是,我在puppeteer的github上找不到它

javascript node.js image buffer puppeteer
1个回答
-1
投票
const downloadImgFromUrl = async (url, dest) => {
    // Create an empty file where we can save data
    const file = fs.createWriteStream(dest);

    // Using Promises so that we can use the ASYNC AWAIT syntax
    await new Promise((resolve, reject) => {
        request({
            uri: url,
            gzip: true,
        }).pipe(file).on('finish', async () => {
            console.log(`Image downloaded from Storage`);
            resolve();
        }).on('error', (error) => {
            reject(error);
        });
    }).catch((error) => {
        console.log(`Image download from URL failed: ${error}`);
    });
}

    await page.waitForTimeout(1000)
    const [filechooser] = await Promise.all([
      page.waitForFileChooser(),
      page.focus("#attach_file_photo"),
      page.click('#attach_file_photo')
    ])

    await downloadImgFromUrl(imgUrl, path.join(os.tmpdir(),'uploadimg.png'))

    await filechooser.accept([path.join(os.tmpdir(), 'uploadimg.png')])
© www.soinside.com 2019 - 2024. All rights reserved.