图片不会上传到我在后端创建的文件夹中

时间:2019-11-28 02:56:07

标签: javascript express vue.js vuejs2 multer

为什么它不将图像上传到我的后端?

下面是我在VueJs中发送代码的代码,该数据将是文件夹名称和图像

uploadFile() {
  var FormData = require("form-data");
  var form = new FormData();
  form.append("ALBUM", "my value");
  form.append(
    "files",
    "C:\\Users\\user\\Desktop\\project\\fileupload\\src\\assets\\logo.png"
  );
  let body = {
    ALBUM: form.get("ALBUM")
  };
  axios.post("http://localhost:9001/file/upload", body);
}

这是我的后端,它试图获取图像并根据前端传递的相册创建一个文件夹

router.post('/upload', (req, res) => {
    let sql = "INSERT INTO GALLERY SET ALBUM = ?";
    let body = [req.body.ALBUM]

    myDB.query(sql, body, (error, results) => {
        if (error) {
            console.log(error);
        } else {
            let directory = `C:/Users/user/Desktop/project/adminbackend/public/${req.body.ALBUM}`;
            fse.mkdirp(directory, err => {
                if (err) {
                    console.log(err);
                } else {
                    const myStorage = multer.diskStorage({
                        destination: directory,
                        filename: function (req, file, cb) {
                            cb(null, file.originalname + path.extname(file.originalname))
                        }
                    });
                    const myUploads = multer({
                        storage: myStorage, limits: {
                            //10 Million
                            fileSize: 10e6
                        }
                    }).array('files', 15);
                    myUploads(req, res, (error) => {
                        if (error) {
                            console.log(error);
                        } else {
                            console.log(req.file);
                            res.send("Success")

                        }
                    });
                }
            })
        }
    })
});

0 个答案:

没有答案