我在我的应用程序上做了一个上传功能。希望它可以帮助
这里是我的成分在我的样本上传功能
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