0
我試圖從客戶端向我的服務器發送數據爲multipart/form-data
但是服務器始終以純文本格式接收數據。這裏是我的代碼:Angular2多部分/表單數據作爲純文本/文本發送,無論選項設置如何
import { Component, ElementRef, ViewChild, Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
@Component({
moduleId: module.id,
selector: 'file-upload',
templateUrl: 'upload.component.html',
})
@Injectable()
export class FileUploadComponent {
@ViewChild('upload-btn') uploadbtn: any;
@ViewChild('inputFile') inputFile: ElementRef;
@ViewChild('progressBar') progressBar: any;
constructor(private http: Http, private e1: ElementRef) { }
/* When button is clicked open input */
upload() {
console.log("clicked");
this.inputFile.nativeElement.click();
} // end of upload
/* Process input form */
sendToServer() {
// Get form input
let inputFile: HTMLInputElement = this.inputFile.nativeElement;
let formData = new FormData();
let fileCount: number = inputFile.files.length;
let headers = new Headers();
headers.append('enctype', 'multipart/form-data');
let options = new RequestOptions({ headers: headers });
console.log(formData);
console.log(options);
console.log(headers);
console.log("File Count: " + fileCount);
if (fileCount > 0) {
for (let i = 0; i < fileCount; i++) {
console.log("Count: " + i);
formData.append('uploads[]', inputFile.files.item(i),
inputFile.files.item(i).name);
}
this.http.post('server IP', formData, options)
.subscribe(data => console.log("Upload Success!"));
}
} // end of send to server
}
我的服務器是IIS
,並獲得了來自angular2
前端POST請求。
謝謝,我會盡快嘗試這個 –
似乎無法得到這個發送文件。 html應該怎麼樣? –
檢查我更新的答案。以下是您在HTML中應該具備的兩項重要內容:
–