【问题标题】:How to pass string as a read stream object in Nodejs?如何在Nodejs中将字符串作为读取流对象传递?
【发布时间】:2019-06-22 17:09:10
【问题描述】:

我正在为第 3 方 api 尝试多重导入功能,我需要传递一个 json 文件读取流。

但我的 api 中已经有一组用户数据,我需要将其作为读取流传递。

我已经尝试过的选项。

  1. fs.createReadStream

fs.createReadStream(Buffer.from(JSON.stringify('[{ user: "data"}]')));

createReadStream 不接受缓冲区和返回

ENOENT:没有这样的文件或目录,打开

  1. require('stream').Readable

    const Readable = require('stream').Readable; const readStream = new Readable(); readStream._read = () => {}; readStream.push(JSON.stringify(u)); readStream.push(null);

读取流在我的请求中出现“无效的多部分有效负载格式”错误

请找到我为多用户文件上传传递的请求对象

{
  "url": "",
  "method": "POST",
  "headers": {},
  "formData": {
    "userFile": ReadStream
  }
}

任何建议都将不胜感激

【问题讨论】:

    标签: node.js filesystems fs


    【解决方案1】:

    您可以像这样从字符串创建可读流:

    import { Readable } from 'stream';
    // OR: const { Readable } = require('stream');
    
    const stream = Readable.from("This is the stream content");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 2021-12-26
      相关资源
      最近更新 更多