Can we somehow rename the file that is being downloaded using puppeteer?

夙愿已清 提交于 2019-11-29 16:44:42

You have two options:

  1. Monitor the requests/responses to log the name of the file and rename it via Node.js
  2. Use the Chrome DevTools Protocol to modify the response header

Option 1: Monitor the requests / response

This is the most straight-forward way to do it. Monitor all responses and in case you notice the response that is being downloaded, use the name to rename it locally via fs.rename.

Code Sample

const path = require('path');

// ...
page.on('response', response => {
    const url = response.request().url();
    const contentType = r.headers()['content-type'];
    if (/* URL and/or contentType matches pattern */) {
        const fileName = path.basename(r.request().url());
        // handle and rename file name (after making sure it's downloaded)
    }
});

The code listens to all responses and wait for a specific pattern (e.g. contentType === 'application/pdf'). Then it takes the file name from the request. Depending on your use case, you might want to check the Content-Disposition header in addition. After that, you have to wait until the file is downloaded (e.g. file is present and file size does not change) and then you can rename it.


Option 2: Use the Chrome DevTools Protocol to modify the response header

I'm 99% sure, that this is possible. You need to intercept the response which is currently not supported by puppeteer itself. But as the Chrome DevTools Protocol is supporting this functionality, you can use it using the low-level protocol.

The idea is to intercept the response and change the Content-Disposition header to your desired file name.

Here is the idea:

  1. Use chrome-remote-interface or a CDP Session to activate Network.requestIntercepted
  2. Listen for Network.requestIntercepted events
  3. Send Network.getResponseBodyForInterception to receive the body of the response
  4. Modify the body and add (or change) the Content-Disposition header to include your filename
  5. Call Network.continueInterceptedRequest with your modified response

Your file should then be save with your modified file name. Check out this comment on github for a code sample. As I already explained it is a rather sophisticated approach as long as puppeteer does not support modifying responses.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!