[英]How to upload files outside root folder in nodejs?
I have my project folder on Ubuntu instance at: /workspace/myproject 我在Ubuntu实例上有以下项目文件夹: / workspace / myproject
Till now, I used to store uploaded files at location: /myproject/public/uploads 到目前为止,我以前将上传的文件存储在以下位置: / myproject / public / uploads
var fs = require('fs');
var savePath = 'public/uploads/';
var filename = uuid.v4() + '.jpg';
var base64 = new Buffer(requestParams[constant.DATA], 'base64');
fs.writeFile(savePath + filename, base64, function(error) {
});
I want to save my uploaded files at: /workspace/uploads 我想将上传的文件保存在: / workspace / uploads
instead of inside the public folder. 而不是放在公用文件夹中。 Also, I don't want to write absolute path in my code.
另外,我不想在代码中写绝对路径。
Any other idea? 还有其他想法吗?
If you want to save a file in a folder within the parent directly, then you could use ../
to go back a level. 如果要将文件直接保存在父级文件夹中,则可以使用
../
返回上一级。 Like this: 像这样:
var fs = require('fs');
var savePath = __dirname + '/../uploads/';
var filename = uuid.v4() + '.jpg';
var base64 = new Buffer(requestParams[constant.DATA], 'base64');
fs.writeFile(savePath + filename, base64, function(error) {
// handle error
});
您可以使用__dirname获取当前目录或./相对路径
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.