我们可以以某种方式重命名使用puppeteer下载的文件吗? [英] Can we somehow rename the file that is being downloaded using puppeteer?

查看:652
本文介绍了我们可以以某种方式重命名使用puppeteer下载的文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过操纵up将文件下载到我的目录中.我需要将此文件上传到s3存储桶,因此我需要选择文件名.但是问题是,此文件名的时间戳每次都会更改,因此我无法保留硬编码的名称.那么有没有办法解决这个问题,以便每次获得一个常量名称(即使替换了旧文件),还是如何重命名正在下载的文件?

I am downloading a file through puppeteer into my directory. I need to upload this file to an s3 bucket so I need to pick up the file name. But the problem is, this file name has a time stamp that changes every time so I can't keep a hard coded name. So is there a way around this to get a constant name every time (even if the old file is replaced), or how to rename the file being downloaded?

我考虑过使用节点的fs.rename()函数,但这将再次需要当前文件名.

I thought of using node's fs.rename() function but that would again require the current file name.

我想要一个恒定的文件名来进行硬编码,然后上传到s3存储桶中.

I want a constant file name to hard code and then upload into the s3 bucket.

  await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: './xml'}); // This sets the directory

  await page.keyboard.press('Tab');

  await page.keyboard.press('Enter'); // This downloads an XML file.

推荐答案

您有两个选择:

  1. 监视请求/响应以记录文件名并通过Node.js重命名
  2. 使用Chrome DevTools协议修改响应标头

选项1:监控请求/响应

这是最简单的方法.监视所有响应,以防万一您注意到正在下载的响应,请使用名称通过 fs.rename .

代码示例

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)
    }
});

代码侦听所有响应并等待特定的模式(例如contentType === 'application/pdf').然后,它从请求中获取文件名.根据您的用例,您可能还需要检查Content-Disposition标头.之后,您必须等到文件下载完毕(例如,文件存在且文件大小未更改),然后才能对其进行重命名.

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.

我有99%的把握,这是可能的.您需要拦截响应,该响应当前是puppeteer本身不支持的响应.但是,由于Chrome DevTools协议支持此功能,因此您可以使用低级协议来使用它.

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.

想法是拦截响应并更改 Content-Disposition 标头指向所需的文件名.

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

这里是个主意:

  1. 使用 chrome-remote-interface
  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

然后,应使用修改后的文件名保存您的文件.查看对github的评论以获取代码示例.正如我已经解释的那样,只要操纵up不支持修改响应,这是一种相当复杂的方法.

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.

这篇关于我们可以以某种方式重命名使用puppeteer下载的文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆