在上传的图像在浏览器中出现变形甚至不完整之后,正在将图像保存到FilesCollection中。
从应用程序获取图像后,temp目录显示完整的图像,但是只有在使用流星文件保存到数据库mongoDB之后,失真才会发生
let Busboy = require("busboy");
let path = require("path"),
os = require("os"),
fs = require("fs");
let tempPath;
WebApp.connectHandlers.use("/upload", (req, res, next) => {
var busboy = new Busboy({
headers: req.headers
});
busboy.on("file", (fieldname, file, filename, encoding, mimetype) => {
tempPath = path.join("/Users/abednegotm/Desktop", path.basename(fieldname));
file.pipe(fs.createWriteStream(tempPath));
console.log(filename);
let size = "";
//retrive file size
file.on("data", data => {
size = data.length;
});
fs.stat(tempPath, (_statError, _statData) => {
const _addFileMeta = {
fileName: filename,
type: mimetype,
size: size
};
//This portion of code that save image to the collection
fs.readFile(tempPath, (_readError, _readData) => {
if (_readError) {
console.log(_readError);
} else {
PropertyImages.write(_readData, _addFileMeta, function(
_uploadError,
_uploadData
) {
if (_uploadError) {
console.log(_uploadError);
} else {
console.log("Data saved to DB");
//unlink or remove file from temporal location after upload
fs.unlink(tempPath, err => {
if (err) throw err;
console.log("file removed from temp");
}); // remove temp upload
}
});
}
});
}); //stat
});
busboy.on("finish", function() {
res.writeHead(200, {
Connection: "close"
});
res.end();
});
return req.pipe(busboy);
});