简体   繁体   English

如何在项目文件夹之外使用 multer 存储数据?

[英]How to store data with multer outside of the project folder?

I want to save a form file outside of the project folder.我想在项目文件夹之外保存一个表单文件。 For that I started using为此我开始使用

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

but __dirname is already inside my project folder.但 __dirname 已经在我的项目文件夹中。 Are there any methods reach out to a path out of the folder?是否有任何方法可以访问文件夹外的路径? Thank you!谢谢!

You can either use the .. notation to go up one folder, or specify a root relative path that starts with / .您可以使用..表示法将 go 向上一个文件夹,或指定以/开头的根相对路径。

Say you want to save your files in the folder located at /home/john/uploads and your project is in /home/john/myproject .假设您要将文件保存在位于/home/john/uploads的文件夹中,并且您的项目位于/home/john/myproject中。 Both of the following would work:以下两种方法都可以:

destination: function (req, file, cb) {
  cb(null,  '/home/john/uploads');
  // or
  cb(null,  '../uploads');
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM