如何更改目录以保存上传文件的角度2?

时间:2022-07-26 22:48:26

I'm using angular 2 and i have an uploader. the default folder to save the file is in /assets folder. is there any way to change this directory to a file server for example? this is my .ts code:

我正在使用角度2,我有一个上传者。保存文件的默认文件夹位于/ assets文件夹中。有没有办法将此目录更改为文件服务器?这是我的.ts代码:

    uploader: FileUploader = new FileUploader({
    url: '/upload/uploadUserImage',
    isHTML5: true
  });

and this is my .js file in backend:

这是我后端的.js文件:

 const storageUserImage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './src/assets/uploads/profile_images/');
  },
  filename: function (req, file, cb) {
    cb(null, file.originalname);
  }
});

const uploadUserImage = multer({
  storage: storageUserImage
}).single('file');
////////////
  module.exports = (router) => {
  router.post('/uploadUserImage', function (req, res) {
    uploadUserImage(req, res, function (err) {
      if (err) {
        res.json({
          error_code: 1,
          err_desc: err
        });
        return;
      }
      res.json({
        error_code: 0,
        err_desc: null
      });
    });
  });

1 个解决方案

#1


0  

Yes according to multer documentation you have destination function in which you may define your path that where it should be stored.

是的,根据multer文档,您有目标函数,您可以在其中定义应存储位置的路径。

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '/../../tmp/my-uploads') // source folder
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now())
  }
})

#1


0  

Yes according to multer documentation you have destination function in which you may define your path that where it should be stored.

是的,根据multer文档,您有目标函数,您可以在其中定义应存储位置的路径。

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '/../../tmp/my-uploads') // source folder
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now())
  }
})