的參數根據文檔 https://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options如何通過一個緩衝區fs.createReadStream
fs.createReadStream()可以接受緩衝液作爲第一個參數
我的節點代碼:
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)
但我得到這個錯誤:
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)
我只想知道,如果我想傳遞一個Buffer作爲路徑參數,我應該提供什麼選項,傳遞'binary'不起作用。
我嘗試它與兩個節點6.11.0和節點8.4.0
請參閱https://stackoverflow.com/questions/13230487/converting-a-buffer-into-a-readablestream-in-nodejs – ivo
文件路徑必須最終成爲操作系統將接受的字符串, t是隨機二進制的,所以目前還不清楚你想通過傳遞一個緩衝區來做什麼。如果你傳遞一個緩衝區,它可能會調用'.toString()'。 – jfriend00
@ivo - 這個帖子與'fs.createReadStream()'的第一個參數無關。 – jfriend00