node.js 如何将缓冲区作为 fs.createReadStream 的参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45891242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:27:46  来源:igfitidea点击:

How to pass a Buffer as argument of fs.createReadStream

node.js

提问by Littlee

According to docs https://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options

根据文档 https://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options

fs.createReadStream() can accept Buffer as first argument

fs.createReadStream() 可以接受 Buffer 作为第一个参数

my node code:

我的节点代码:

var _ = require('lodash')
var faker = require('faker')
var http = require('http')
var fs = require('fs')
var xlsx = require('node-xlsx')

var gg = _.range(10).map((item) => {
  return _.range(10).map((item) => {
    return faker.name.findName()
  })
})

http.createServer(function(req, res) {
  var buf = xlsx.build([{
    name: 'sheet1',
    data: gg
  }])
  fs.createReadStream(buf, 'binary').pipe(res)

}).listen(9090)

but I get this error:

但我收到此错误:

events.js:160
  throw er; // Unhandled 'error' event
  ^

Error: Path must be a string without null bytes
at nullCheck (fs.js:135:14)
at Object.fs.open (fs.js:627:8)
at ReadStream.open (fs.js:1951:6)
at new ReadStream (fs.js:1938:10)
at Object.fs.createReadStream (fs.js:1885:10)
at Server.<anonymous> (/Users/xpg/project/test/index.js:18:6)
at emitTwo (events.js:106:13)
at Server.emit (events.js:191:7)
at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:546:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)

enter image description here

在此处输入图片说明

I just want to know that if I want to pass a Buffer as the path argument, what is the options I should provide, passing 'binary' doesn't work.

我只想知道,如果我想将 Buffer 作为路径参数传递,我应该提供哪些选项,传递“二进制”不起作用。

I try it with both Node 6.11.0 and Node 8.4.0

我在 Node 6.11.0 和 Node 8.4.0 上都尝试过

回答by jfriend00

The first argument to fs.createReadStream()must be the file path. You can apparently pass the path in a Buffer object, but it still must be an acceptable OS path when the Buffer is converted to a string.

的第一个参数fs.createReadStream()必须是文件路径。您显然可以在 Buffer 对象中传递路径,但是当 Buffer 转换为字符串时,它仍然必须是可接受的操作系统路径。

It appears you are trying to pass the file content to fs.createReadStream(). That is not how that API works. If you look into the code for fs.createReadStream()it is completely clear in the code that it is going to call fs.open()and pass the first argument from fs.createReadStream()as the file path for fs.open().

您似乎正在尝试将文件内容传递给fs.createReadStream(). 这不是该 API 的工作方式。如果您查看代码,fs.createReadStream()它在代码中完全清楚,它将调用fs.open()并将第一个参数fs.createReadStream()作为fs.open().

If what you're trying to do is to create a readable stream from a buffer (no file involved), then you need to do that a different way. You can see how to do that here: How to wrap a buffer as a stream2 Readable stream?

如果您想要做的是从缓冲区(不涉及文件)创建可读流,那么您需要以不同的方式来做。您可以在此处了解如何执行此操作:如何将缓冲区包装为 stream2 可读流?

回答by shaochuancs

@jfriend00 has already provided a very clear explanation on this issue. If a Buffer object is passed as argument to fs.createReadStream(), it should indicate the file path, not file content. As @Littlee asked in comment, here is an example code:

@jfriend00 已经对这个问题提供了非常明确的解释。如果 Buffer 对象作为参数传递给 fs.createReadStream(),它应该指示文件路径,而不是文件内容。正如@Littlee 在评论中所问的,这是一个示例代码:

var express = require('express');
var router = express.Router();
var fs = require('fs')

router.get('/test', function(req, res) {
  var buf = Buffer.from('./test.html');
  fs.createReadStream(buf).pipe(res);
});

Please note the Buffer bufindicates a file path ("./test.html"), not the file test.html's content.

请注意 Bufferbuf表示文件路径 ( "./test.html"),而不是文件test.html的内容。