gpt4 book ai didi

javascript - ENOENT 错误 - 我已成功上传文件,但无法移动它们

转载 作者:太空宇宙 更新时间:2023-11-04 02:34:15 25 4
gpt4 key购买 nike

我已成功将文件上传到临时目录,并希望将它们移动到目录 profile_pictures。这看起来是一件简单的事情,但我却被困在这里一个小时了!

enter image description here

使用 Express 和 fs 执行此操作的非常简单的代码:

app.post('/upload', function (req, res, next) {
console.log("User uploading profile picture...");

var tmp_path = req.files.profile_picture.path; // get the temporary location of the file
var ext = path.extname(req.files.profile_picture.name); // get the extension of the file with the path module

var target_path = '/profile_pictures/' + req.body.username + ext; // set where the file should actually exists - in this case it is in the "images" directory

fs.rename(tmp_path, target_path, function (err) { // move the file from the temporary location to the intended location
if (err) throw err;

fs.unlink(tmp_path, function (err) { // delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files
if (err) throw err;
res.send('File uploaded to: ' + target_path + ' - ' + req.files.profile_picture.size + ' bytes');
});
});
});

但这会导致错误:

Error: ENOENT, rename 'tmp/5162-2fftn.jpg'] errno: 34, code: 'ENOENT', path: 'tmp/5162-2fftn.jpg'

顶部的图像是我的 SFTP 管理器连接到此应用程序的工作目录的屏幕截图,因此显然该目录确实存在!

我的错误是什么?

最佳答案

我自己花了几个小时来解决这个问题。看一下这里以获得澄清, https://github.com/nodejs/node-v0.x-archive/issues/2703

这里的线程实际上为我指明了正确的方向, Move File in ExpressJS/NodeJS

是的,fs.rename 不会在两个不同的磁盘/分区之间移动文件。这是正确的行为。 fs.rename 提供与 Linux 中的 rename(2) 相同的功能。

关于javascript - ENOENT 错误 - 我已成功上传文件,但无法移动它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23986625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com