【发布时间】:2019-01-09 12:34:21
【问题描述】:
我是 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 命名空间调用 import 并在构造函数中创建一个私有客户端参数并解决此问题,但这似乎也不起作用。
我知道 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, ' ', ' ', ' ', ' ', ' ');
}
【问题讨论】:
标签: node.js angular typescript constructor ftp-client