此处的节点js代码
var express = require('express');
var multer = require('multer');
var path = require('path');
var sftpStorage = require('multer-sftp')
var app = express();
var port = 3000;
// specify the folder
app.use(express.static(path.join('/home/g457b5jxaq4m/public_html/shiva/upload/, 'uploads')));
// headers and content type
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
var storage = sftpStorage({
sftp: {
host: 'ip address',
port: 22,
username: 'username',
password: 'password'
},
// destination
destination: function (req, file, cb) {
cb(null, './uploads/')
},
filename: function (req, file, cb) {
cb(null, file.originalname);
}
});
var upload = multer({ storage: storage });
app.post("/upload", upload.array("uploads[]", 12), function (req, res) {
console.log('files', req.files);
res.send(req.files);
});
var server = app.listen(port, function () {
console.log("Listening on port %s...", port);
});
角度代码:
export class NotfoundComponent implements OnInit {
filesToUpload: Array<File> = [];
upload() {
const formData: any = new FormData();
const files: Array<File> = this.filesToUpload;
console.log(files);
for(let i =0; i < files.length; i++){
formData.append("uploads[]", files[i], files[i]['name']);
}
console.log('form data variable : '+ formData.toString());
this.http.post('http://localhost:3000/upload', formData)
.subscribe(files => console.log('files', files))
}
fileChangeEvent(fileInput: any) {
this.filesToUpload = <Array<File>>fileInput.target.files;
}
constructor(private http: HttpClient) { }
ngOnInit() {
}
}
HMTL
<input id="cin" name="cin" type="file" (change)="fileChangeEvent($event)" placeholder="Upload a file..." multiple/>
<button type="button" class="btn btn-success btn-s" (click)="upload()">
<i class="glyphicon glyphicon-open-file"></i> Upload
</button>
控制台问题
我正在尝试借助Nodejs在远程服务器中上传文件。 前端 Angular 5 后端 Node js 我可以在本地服务器上上传,但是当我尝试在远程FTP响应中上载时,似乎出现链接内部错误。无法加载以下本地服务器节点API