我是 Angular/Node 新手,似乎无法找出我遇到的问题。
我正在尝试使用 client-ftp 连接到 ftp并且在实现方面遇到了麻烦。本质上,我在前端创建一个按钮,如下所示:
<button class="btn btn-primary" (click)="downloadFile()"><i class="fa fa-file-photo-o"></i> Download Screenshot</button>
并尝试通过这样的点击事件来实现它:
downloadFile(){
console.log('Connecting to sftp...');
var ftpClient = require('ftp-client'),
config = {
host: 'localhost',
port: 22,
user: 'anonymous',
password: 'anonymous'
},
options = {
logging: 'basic'
},
client = new ftpClient(config, options);
console.log('Downloading Screenshot...');
client.connect(function () {
client.download('/sftFilepPath', './Downloads', {
overwrite: 'none'
}, function (result) {
console.log(result);
});
});
}
我不断收到错误,但显示:TypeError: Socket is not a constructor
每当按下按钮时。我的两个 console.log() 调用都显示在调试器中,因此我认为它与我尝试实际连接和下载的位置有关。
我尝试调用 FtpClient 命名空间的导入,并在构造函数中创建一个私有(private)客户端参数并对其进行处理,但这似乎也不起作用。
我知道 new 关键字隐式调用构造函数,但我没有在代码中的任何地方引用“Socket”。也许我错过了什么?
这是我的导入和构造函数供引用:
import { Component, ElementRef, Input, OnInit, OnDestroy } from '@angular/core';
import { TestInstance, Test, TestDetails } from '../../objModules/index';
import { RunServices } from './run.services';
import { Observable } from 'rxjs/Rx';
import * as $ from 'jquery';
import * as _ from 'underscore';
import { ModalController } from './run.modal.controller';
// import { FtpClient } from 'ftp-client';
@Component({
templateUrl: './run.modal.component.html',
moduleId: module.id.toString(),
selector: 'app-test-data',
providers: [ RunServices ]
})
export class ModalComponent implements OnInit, OnDestroy {
testInstance: TestDetails;
id: string;
private element: JQuery;
private runId: string;
private test$: Observable<Test>;
private testHistory$: Observable<TestInstance[]>;
private test: Test;
private testHistory: TestInstance[];
private selectedTest: TestInstance;
private latestRuns: TestInstance[];
private averagePass: number;
private averageFail: number;
services: RunServices;
constructor(private modalController: ModalController, private el: ElementRef, private runServices: RunServices) {
this.element = $(el.nativeElement);
this.services = runServices;
// Initialize these objects so we don't get any errors
this.test = new Test(' ', ' ', ' ', [' '], ' ', ' ');
this.testHistory = new Array<TestInstance>(new TestInstance(' ', ' ', ' ', 0, ' ', ' ', ' ', ' ', ' '));
this.testInstance = new TestDetails(' ', ' ', ' ', ' ', ' ', ' ', ' ', 0, null, ' ');
this.selectedTest = new TestInstance(' ', ' ', ' ', 0, ' ', ' ', ' ', ' ', ' ');
}
最佳答案
我的猜测是您的 ftp 客户端太旧了。系统环境变化越来越快。但这个客户端自2015年以来就没有更新过。尝试使用最新的客户端,例如jsftp
关于node.js - 类型错误 : Socket is not a constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51641736/