ayqy / string-to-file-stream

NodeJS create file stream from string.
http://www.ayqy.net/blog/node-js%e4%bb%8e%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%94%9f%e6%88%90%e6%96%87%e4%bb%b6%e6%b5%81/
MIT License
19 stars 7 forks source link

ERR_UNHANDLED_ERROR #1

Open zhoocoo opened 4 years ago

zhoocoo commented 4 years ago

I get errors like this below:

Error [ERR_UNHANDLED_ERROR]: Unhandled error.
    at DestroyableTransform.EventEmitter.emit (domain.js:453:17)
    at DestroyableTransform.onerror (D:\workSpace\front-end\gulpSpace\node_modules\readable-stream\lib\_stream_readable.js:640:52)
    at DestroyableTransform.emit (events.js:198:13)
    at DestroyableTransform.EventEmitter.emit (domain.js:448:20)
    at uploadX.then.catch (D:\workSpace\front-end\gulpSpace\utils\gulp-x-upload.js:77:14)
    at process._tickCallback (internal/process/next_tick.js:68:7)

and my code like this:

const request = require("request");

const string2fileStream = require('string-to-file-stream');

const isFileStream = require('is-file-stream');

.......

const r = request.post(_apiUrl, (err, httpResponse, body) => {});

    const form = r.form();

    form.append("username", uploadConfig.username);

    form.append("password", uploadConfig.password);

    form.append("bucket", "x.autoimg.cn");

    form.append("dirname", "dealer"); 

    form.append("fpath", path.dirname(uploadPath));

    const streamFile = string2fileStream(chunk.contents.toString())

    console.log(isFileStream(streamFile));

    console.log(chunk.contents.toString());

    form.append("file", streamFile);

the chunk is belone to through2.obj(function(chunk, encoding, cb)

please help me to reslove this problem, I can't transform the string to a filestream.

ayqy commented 4 years ago

I have tested these code locally, and it works well, there are some points:

const streamFile = string2fileStream(new Buffer('12344').toString(), { path: 'no-this-file.txt' })
console.log(isFileStream(streamFile));
console.log(new Buffer('12344').toString());
// output
true
12344

Hope this will be helpful for you.