fs.rename 无效,但原始名称和目标名称路径似乎正确

fs.rename not operative, however original and target name paths seem correct

我正在使用 Node.js 和 Multer 将一些图像文件上传到 "uploads" 文件夹。那部分似乎工作正常。我希望在上传后重命名文件,以便将正确的文件扩展名(.jpg、.png、.gif 等)附加到文件名中。
我正在尝试使用 'fs.rename' 来完成此操作,但它失败了。我没有收到任何错误...并且 'original' 路径和 'target' 重命名路径在我检查控制台日志时似乎是正确的。我的代码在下面......有人对此有解释吗?这很混乱,我提前谢谢你。

  //A means of ensuring only images are uploaded.
  //note 'files' is an array of image files, omitted for clarity

   var len = files.length;
   var i;

   for (i = 0; i < len; i++) {

    if (files[i] != "undefined") {

    const host = req.hostname;
    const filePath = req.protocol + "://" + host + '/' + files[i].path;
    const image = files[i].mimetype.startsWith('image/');
    const type = files[i].mimetype.toString();

     if(image) {
     console.log('photo #' + i + ' uploaded');
     console.log('uploaded file: ' + files[i].filename + ' saved within: ' + files[i].destination + ' at path: ' + files[i].path);

     console.log('photo #' + i + ' filepath: ' + filePath);
     console.log('photo #' + i + ' image extension is: ' + type);
     console.log('photo #' + i + ' TYPEOF is: ' + typeof type);
     var cutoff = type.split("/");  //"split" on "backslash"

     var oldPath = req.protocol + "://" + host + ':8080' + '/uploads/' + files[i].filename
     var targetPath = req.protocol + "://" + host + ':8080' + '/uploads' + '/image' + i + "." + cutoff[1]

     console.log('photo #' + i + ' cutoff: ' + cutoff[1]);
     console.log('ORIGINAL path for photo #' + i + ' is: ' + oldPath);
     console.log('RENAMED target path for photo #' + i + ' is: ' + targetPath);

      fs.rename(oldPath, targetPath, function(err) {
       if (err) {
        console.log("Unable to rename photo #" + i + " file...!")
       } else {
        console.log("Successfully renamed the file!")
       }
      }) 

     // ...save filePath to database...if desired...

     } else {
     console.log("file # " + i + " received--however wrong format");
     }

    }  //if NOT 'undefined'

   }  //for loop

这是 运行 显示控制台日志的输出:

photo #0 uploaded
uploaded file: identification-1572752074000 saved within: ./uploads at path: uploads\identification-1572752074000
photo #0 filepath: http://localhost/uploads\identification-1572752074000 
photo #0 image extension is: image/jpeg
photo #0 TYPEOF is: string
photo #0 cutoff: jpeg
ORIGINAL path for photo #0 is: http://localhost:8080/uploads/identification-1572752074000
RENAMED target path for photo #0 is: http://localhost:8080/uploads/image0.jpeg
photo #1 uploaded
uploaded file: identification-1572752074015 saved within: ./uploads at path: uploads\identification-1572752074015
photo #1 filepath: http://localhost/uploads\identification-1572752074015
photo #1 image extension is: image/jpeg
photo #1 TYPEOF is: string
photo #1 cutoff: jpeg
ORIGINAL path for photo #1 is: http://localhost:8080/uploads/identification-1572752074015
RENAMED target path for photo #1 is: http://localhost:8080/uploads/image1.jpeg
Unable to rename photo #2 file...!
Unable to rename photo #2 file...!

另一个奇怪的事情是循环前进到 'photo #2'...这不应该是因为 'files' 数组中只有 2 个图像('0' 和 '1')。 ..不确定这是否与我的问题有关。感谢您的任何建议。

您正在将 URL 传递给 fs.rename()。它需要本地 OS 文件路径,而不是 URL。

我不知道你的本地文件系统中的确切位置,或者它们是否在你的本地文件系统中,但你可以尝试更改它:

 var oldPath = req.protocol + "://" + host + ':8080' + '/uploads/' + files[i].filename
 var targetPath = req.protocol + "://" + host + ':8080' + '/uploads' + '/image' + i + "." + cutoff[1]

对此:

 var oldPath = '/uploads/' + files[i].filename
 var targetPath = '/uploads' + '/image' + i + "." + cutoff[1]

假设 /uploads 目录在本地文件系统上并且 /uploads/image 目录已经存在。