Skip to main content

download-file

To download file follow next model:

module.exports = async function (page) {
await page.goto('url');
const downloadPromise = page.waitForEvent('download');
await page.locator('button-that-start-download').click();
const download = await downloadPromise;
return {
File: {
name: 'test.pdf',
file: 'data:application/pdf;base64,' + Buffer.from(fs.readFileSync(await download.path())).toString('base64'),
mime_type: 'application/pdf'
}
}
}

1) Register download event before initiate download event (before click on download button)

  const downloadPromise = page.waitForEvent('download');

2) Initiate download and wait for promise

  await page.locator('button-that-start-download').click();
const download = await downloadPromise;

3) In order to get file location you can run

await download.path()

4) To show view file button on the right side of the builer page, your file should be in the next format

  {
File: {
name: '',
file: '',
mime_type: ''
}
}