简体   繁体   English

请求:如何通过管道传输文件并保留响应对象

[英]request: how to pipe file and keep the response object

I use npm request.我使用 npm 请求。 I want to download and write a file to the filesystem and after that-use the returned response object for further processing.我想下载一个文件并将其写入文件系统,然后使用返回的响应对象进行进一步处理。

I know that I can pipe the response object directly but afterwards I have no response object more for further processing我知道我可以直接管道响应对象,但之后我没有更多的响应对象进行进一步处理

var result = await request(builtReq).pipe(fs.createWriteStream(myFilePath));

So my implementation at the moment looks like this:所以我现在的实现是这样的:

var result = await request(builtReq);

But I am not able to pipe the result object because the streamable state is false.但是我无法通过管道传输结果对象,因为可流式传输状态为 false。

So I need a way to keep the return of the request and to write the file to the filesystem.所以我需要一种方法来保持请求的返回并将文件写入文件系统。 Can I reset the stream state or somehow keep the response obj and write the file at the same time?我可以重置流状态或以某种方式保留响应 obj 并同时写入文件吗? I tried to implement the write file manually via.我试图通过手动实现写入文件。 fs.writeFile() after I received the response obj but I had problems with file encodings because I can receive anything and I ended up with broken files. fs.writeFile() 在我收到响应 obj 后,但我遇到了文件编码问题,因为我可以接收任何东西,但最终我得到了损坏的文件。

Has somebody an idea how to solve that?有人知道如何解决这个问题吗?

Do you want the response object (this contains the status code and headers), or do you need the response body in-memory? 您是否需要响应对象(其中包含状态代码和标头),还是在内存中需要响应主体?

If you only want the response object, and still want the body written to a file, you can grab the response like so: 如果您只想要响应对象,但仍希望将主体写入文件,则可以像这样获取响应:

var response;

await request(builtReq).on('response', r => {
    console.log('Response received: ' + r);
    response = r;
}).pipe(fs.createWriteStream(myFilePath));

console.log('Finished saving file, response is still ' + response);

If you need to process the actual body, you have a couple options: 如果需要处理实际主体,则有两种选择:

  • Keep your existing code as-is, then just read the file off the disk right after it finishes (since you are using await , that would be the next line) and process it in memory. 保持现有代码不变,然后在完成后立即从磁盘上读取文件(因为您正在使用await ,这将是下一行),然后在内存中对其进行处理。

  • Pipe the input stream to multiple places -- one to the file write stream, and the other to an in-memory buffer (using a standard on('data') handler). 将输入流传递到多个位置-一个传递到文件写入流,另一个传递到内存缓冲区(使用标准的on('data')处理函数)。 Once the stream finishes, the file is saved and you can process the in-memory buffer. 流完成后,文件被保存,您可以处理内存中的缓冲区。 See related question node.js piping the same readable stream for several different examples. 有关几个不同的示例,请参见相关问题node.js传递相同的可读流

remember promises can only be resolved once.记住承诺只能解决一次。

so to solve your problem:所以要解决你的问题:

var promise = request(builtReq)

// now do what ever number of operations you need
result.pipe(fs.createWriteStream(myFilePath));

promise.then(r=>console.log('i will still get data here :)');
promise.then(r=>console.log('and here !');
promise.then(r=>console.log('and here !!');

var result = await promise; // and here to !!

a promise once resolve, the .then will always get the result.承诺一旦解决, .then将始终得到结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM