简体   繁体   English

如何在nodejs的根文件夹之外上传文件?

[英]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.

相关问题 NodeJS / CloudFoundry-失败:应用程序上传无效:Symlink指向根文件夹之外 - NodeJS/CloudFoundry - failed: The app upload is invalid: Symlink(s) point outside of root folder 谷歌驱动器 API:无法使用 nodejs 将文件上传到根文件夹 - Google drive API: can't upload files to the root folder using nodejs 如何在项目文件夹之外使用nodejs存储数据? - How to store data with nodejs outside of the project folder? 如何在通过multer上传文件之前自动创建文件夹,以便这些文件存储在nodejs中创建的文件夹中? - How to create folder automatically before files upload via multer so that those files get stored in that created folder in nodejs? 如何在nodeJs中使用cli上传文件夹中的文件 - how to upload file in folder using cli in nodeJs 如何使用 NodeJs Multer 上传 2 个图像不同的 2 个文件夹? - How to upload 2 image different 2 folder with NodeJs Multer? 如何将某些Node.js文件放置在Node.js应用程序根文件夹之外? - How can I place some of my Node.js files outside of my Node.js application root folder? 将照片上传到Node.js的文件夹中 - Upload photo into a folder in nodejs 我如何才能确保 nodejs pkg 包含我的所有文件? 可执行文件在根文件夹中运行,但不在其他任何地方 - How can I just make sure that nodejs pkg is including all of my files? Executable runs in root folder but doesn't anywhere else 如何在 React 应用程序中获取根文件夹之外的文件的路径 - How to get the path to a file outside the root folder in React application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM