javascript - 上传后流星文件扭曲图像
问题描述
上传图像在浏览器中出现扭曲或不完整后,正在将图像保存到 FilesCollection。
从应用程序获取图像后,临时目录显示完整的图像,但只有在使用流星文件保存到数据库 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);
});
解决方案
推荐阅读
- php - Microsoft Dynamics crm 按 accountid 和通过 api 联系的姓名过滤
- amazon-web-services - AWS Auth 参数在多个环境中被覆盖
- php - 当我们从其他支持者调用 url 时如何防止它不应该在 laravel 中附加我当前的 url
- reactjs - 带异步的 React-router-dom 保护路由
- html - 保持文本在容器内垂直居中
- azure - 将 Azure SQL 数据库与 Azure Cosmos DB MongoDB API 同步
- nginx - nginx $upstream_status 和 $status 有什么区别
- java - 为什么在将 Integer 转换为 double 时计算函数不给出错误?
- python - 如何结合这两个代码?(python图像查看器+鼠标拖动图像ROI裁剪)
- php - 关系有更多关于 laravel 获得双胞胎结果