【发布时间】:2021-04-03 15:25:24
【问题描述】:
我在此链接 (https://master.d3tei1upkyr9mb.amplifyapp.com/report) 上有一个带有 3 个导出按钮的页面。 这些导出按钮在前端生成 XLSX、CSV、PDF,因此没有 XLSX、CSV、PDF 的 URL。
我需要 puppeteer 能够在我的节点后端下载或获取或拦截这些文件的 blob 或缓冲区。
我尝试了不同的方法来实现这一点,但仍然没有弄清楚。
通过 playwright 库通过下面编写的代码可以实现。但我需要能够用 Puppeteer 做到这一点。
const {chromium} = require('playwright');
const fs = require('fs');
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext({acceptDownloads: true});
const page = await context.newPage();
await page.goto('http://localhost:3000/');
const [ download ] = await Promise.all([
page.waitForEvent('download'), // <-- start waiting for the download
page.click('button#expoXLSX') // <-- perform the action that directly or indirectly initiates it.
]);
const path = await download.path();
console.log(path);
const newFile = await fs.readFileSync(path);
console.log(newFile);
fs.writeFile("test.xlsx", newFile, "binary",function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
await browser.close()
})();
有什么办法吗?
【问题讨论】:
-
对于上下文,在我的情况下,直到用户单击链接(他们正在使用 react-csv)才真正生成 blob。我不能使用
fetch()请求,因为 url/blob 是空的,因为没有点击任何链接。 -
我删除了我的答案,因为它只适用于 xlsx,但你看到了我希望的总体思路;-;
标签: node.js puppeteer google-chrome-headless