Node js Promise-ftp:上传文件时出错:错误:无法打开该文件:没有这样的文件或目录

问题描述 投票:0回答:1

使用node js服务器和promise-ftp库,我想将文件“inventory.txt”上传到FTP服务器。

这些是进口产品:

const fs = require("fs");
const path = require("path");
const PromiseFtp = require("promise-ftp");
const ftp = new PromiseFtp();

这是代码:

      const filePath = path.join(__dirname, "./inventory.txt");

      // send file to FTP server
      const { host, userName, pwd, destPath } = sails.config.ftpkey;
     
      // check if file exists
      if (!fs.existsSync(filePath)) return res.status(500).send("File not found. Please try again.");
      else console.log("File exists");

      ftp
        .connect({
          host: host,
          user: userName,
          password: pwd,
        })
        .then(function (serverMessage) {
          console.log("Server message: " + serverMessage);
          return ftp.put("./inventory.txt", destPath);
        })
        .then(function (serverMessage) {
          console.log("File uploaded successfully");
          return ftp.end(); // Close the FTP connection
        })
        .then(function () {
          res.send("File uploaded");
        })
        .catch(function (err) {
          console.error("Error uploading file:", err);
          res.status(500).send("Error uploading file");
        })
        .finally(() => {
          ftp.end();
        });

这是安慰的回应:

File exists
Server message: --------- Welcome to Pure-FTPd [privsep] [TLS] ----------
You are user number 1 of 45 allowed.
Local time is now 10:57. Server port: 21.
This is a private system - No anonymous login
IPv6 connections are also welcome on this server.
You will be disconnected after 15 minutes of inactivity.
Error uploading file: Error: Can't open that file: No such file or directory
    at makeError (D:\USquare\Carzlane_Management_System\carzlane\node_modules\@icetee\ftp\lib\connection.js:1128:13)
    at Parser.<anonymous> (D:\USquare\Carzlane_Management_System\carzlane\node_modules\@icetee\ftp\lib\connection.js:122:25)
    at Parser.emit (node:events:513:28)
    at Parser.emit (node:domain:489:12)
    at Parser._write (D:\USquare\Carzlane_Management_System\carzlane\node_modules\@icetee\ftp\lib\parser.js:61:10)
    at writeOrBuffer (node:internal/streams/writable:392:12)
    at _write (node:internal/streams/writable:333:10)
    at Writable.write (node:internal/streams/writable:337:10)
    at Socket.ondata (D:\USquare\Carzlane_Management_System\carzlane\node_modules\@icetee\ftp\lib\connection.js:298:20)
    at Socket.emit (node:events:513:28)
    at Socket.emit (node:domain:489:12)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
    at TCP.callbackTrampoline (node:internal/async_hooks:130:17) {
  code: 553
}

据我所知,FTP 配置是正确的,但我无法上传我的文件

注意: 由于 inventory.txt 文件位于同一文件夹中,因此我尝试了这两种方法 ftp.put(filePath, destPath); & ftp.put("./inventory.txt", destPath);

javascript node.js ftp sails.js
1个回答
0
投票

代码运行良好。实际问题是由于 destPath 造成的。

我试图提供服务器的绝对路径,例如:xyz/abc/inventory.txt

相反,我需要做的就是 ./inventory.txt

© www.soinside.com 2019 - 2024. All rights reserved.