【发布时间】:2018-05-01 14:13:21
【问题描述】:
我正在开发一个必须组织一些上传文件的应用程序,我将它们分隔在具有请求 ID 的文件夹中(我正在使用 express-request-id 来获取此 ID)。
问题是每次我有多个文件时,“移动”过程都会失败,我似乎无法修复它。
let request_folder = path.resolve(tmp_folder + "/" + req.id);
/* Checking if the folder exists */
fs.access(request_folder, fs.constants.F_OK, function(error) {
if(error) { // it doesn't
/* Trying to create it */
fs.mkdir(request_folder, function(error) {
if(error) {
console.log("Error: Couldn't create the directory.");
console.log(error);
}
});
}
});
/* Moving uploaded files to their respective request folder */
req.files.forEach(function(file) {
let new_file_path = path.resolve(request_folder + "/" + file.filename);
fs.rename(file.path, new_file_path, function(error) {
if(error) {
console.log("Error: Couldn't move " + file.filename + ".");
console.log(error);
}
});
});
我百分百确定文件夹和文件都存在,但是当我尝试一次移动两个文件时,我得到:
Error: Couldn't move Desert.jpg.
{ Error: ENOENT: no such file or directory, rename 'C:\Users\telmo.silva\csc-links\public\tmp\Desert.jpg' -> 'C:\Users\telmo.silva\csc-links\public\tmp\d2abf375-d09f-440c-a5ba-adf4f5725a73
\Desert.jpg'
errno: -4058,
code: 'ENOENT',
syscall: 'rename',
path: 'C:\\Users\\telmo.silva\\csc-links\\public\\tmp\\Desert.jpg',
dest: 'C:\\Users\\telmo.silva\\csc-links\\public\\tmp\\d2abf375-d09f-440c-a5ba-adf4f5725a73\\Desert.jpg' }
Error: Couldn't move Chrysanthemum.jpg.
{ Error: ENOENT: no such file or directory, rename 'C:\Users\telmo.silva\csc-links\public\tmp\Chrysanthemum.jpg' -> 'C:\Users\telmo.silva\csc-links\public\tmp\d2abf375-d09f-440c-a5ba-adf4f
5725a73\Chrysanthemum.jpg'
errno: -4058,
code: 'ENOENT',
syscall: 'rename',
path: 'C:\\Users\\telmo.silva\\csc-links\\public\\tmp\\Chrysanthemum.jpg',
dest: 'C:\\Users\\telmo.silva\\csc-links\\public\\tmp\\d2abf375-d09f-440c-a5ba-adf4f5725a73\\Chrysanthemum.jpg' }
有谁知道我做错了什么?谢谢!
【问题讨论】:
标签: javascript node.js express multer