需求:整个需求可以抽象为Node.js通过readSteam读取二进制文件并通过post将其发送到后端服务器存储到mongodb中。目前的问题:通过createReadStream创建的读流中读取数据,返回的为chunk类型的数据,通过Node.js中的post上传时,数据丢失。代码如下:/** * Created by Administrator on 2018/4/29. */var fs = require('fs');var http = require("http");var queryString = require("querystring")var filepath = "./mmp.txt";var readSteam = fs.createReadStream(filepath);readSteam.on("data",(chunk) => { console.log(chunk); let mydata = {"name":filepath, data: chunk}; console.log(123) console.log(mydata); doapost(mydata);})function doapost(data) { let contents = queryString.stringify(data); console.log("here"); console.log(contents); let options = { host: "localhost", path: "/mytestpost/", port: 8000, method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': contents.length } }; let req = http.request(options, function (res) { res.on("data", function (chunk) { console.log(chunk.toString()) }); res.on("end", function (d) { console.log("end") }); res.on("error", function (e) { console.log(e); }) }); req.write(contents); req.end();}运行结果如下:可以看到上传的时候name=.%2Fmmp.txt&data=处buffer数据丢失了,请问各位码友该如何解决?
添加回答
举报
0/150
提交
取消