首页 > 解决方案 > 上传后流星文件扭曲图像

问题描述

上传图像在浏览器中出现扭曲或不完整后,正在将图像保存到 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);
    });

标签: javascriptnode.jsexpressreact-nativemeteor

解决方案


推荐阅读