我在我的應用程序上做了一個上傳功能。希望它可以幫助
這裏是我的成分在我的樣本上傳功能
uploadDatasource(fileInput: any) {
let file = fileInput.target.files[0];
let fileName = file.name;
let payload = {
file,
}
let formData: FormData = new FormData();
formData.append('file',file,file.name);
this.DsListService.uploadDatasource(formData)
.subscribe(
response => {
console.log('UPLOADING success');
},
error => {
console.log('error',error)
});
}
這裏是我的服務類
import { Injectable } from '@angular/core';
import { Headers, Http, RequestOptions, Response, URLSearchParams } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { Config } from '../config/config';
@Injectable()
export class DsListService {
private config = new Config;
constructor(private http: Http) { }
uploadDatasource(payload): Observable<any[]> {
let headers = new Headers();
headers.append('Accept', 'application/json, text/plain,');
let options = new RequestOptions({ headers: headers });
return this.http.post(`API_UPLOAD_PATH`,payload, options)
.map((res: Response) => {
let data = res.json();
return data;
})
.catch(error => Observable.throw(error))
}
}
,這裏是我的html
<input type="file" [(ngModel)]="Model.datasourcelistdata" name="datasource_upload" id="datasource_upload" accept=".xlsx,.xls,.csv" ngf-max-size="20MB" fd-input (change)="uploadDatasource($event)" />
這與[這個問題(https://開頭計算器的.com /問題/ 40214772 /文件上傳入 - 角-2)。你可以用[這個笨蛋的例子]來測試(https://plnkr.co/edit/S2jycxeyLLLFu5wFDtA8?p=preview) – 0mpurdy
https://coderexample.com/reading-csv-file-using-javascript/完全使用pappa parse來閱讀csv – jemiloii