首页 > 解决方案 > 使用 node js 将文件上传到 ftp 服务器

问题描述

我正在尝试使用节点 js 将文件从本地机器上传到 ftp 服务器。以下是我尝试过的代码。我收到错误为“getaddrinfo ENOTFOUND https://owncloud.askgroup.co.in/remote.php/dav/files/jlotlikar/”

 class FTPClient {
        constructor(host = 'localhost', port = 21, username = 'anonymous', password = 'guest', secure = false) {
            this.client = new ftp.Client();
            this.settings = {
                host: host,
                port: port,
                user: username,
                password: password,
                secure: secure
            };
        }
    
        upload(sourcePath, remotePath, permissions) {
            let self = this;
            (async () => {
                try {
                    let access = await self.client.access(self.settings);
                    let upload = await self.client.upload(fs.createReadStream(sourcePath), remotePath);
                    let permissions = await self.changePermissions(permissions.toString(), remotePath);
                } catch(err) {
                    console.log(err);
                }
                self.client.close();
            })();
        }
    
        close() {
            this.client.close();
        }
    
        changePermissions(perms, filepath) {
            let cmd = 'SITE CHMOD ' + perms + ' ' + filepath;
            return this.client.send(cmd, false);
        }
    }
    
    const client = new FTPClient('https://owncloud.askgroup.co.in/remote.php/dav/files/jlotlikar/', 443, 'jlotlikar', 'June@2020', false);

    client.upload('./cid.xml', 'HRMS_ExpenseReport/upload.xml', 443);

我无法连接到服务器。我是节点和 ftp 传输的新手。有人可以帮助我连接到 ftp 服务器并通过节点 js 上传文件吗?

标签: node.jsftpftp-client

解决方案


推荐阅读