首页 > 解决方案 > 发布 csv 上传的文件,通过 body 其名称和内容 Angular

问题描述

我有一个带有 data-import.service、data-import 组件及其子组件的 Data-Import-Module:uploader.component(在上传器模块中定义了自己的服务) save.component(button) 和 display-table.component (按钮)和 table.component。

我想上传 csv 文件(我想它是带有点的表,Point(x;y;)),使用 httpClient.post 方法将其保存到服务器,其中传递 body{fileName, content-of-csv-parsed to-json或者只是一个表格},显示一个包含上传文件内容的表格。

httpClient 方法对我来说是新的,我真的不明白什么是 headers 和 param,它们是 post 方法中的第三个(选项)参数——我应该把 fileName 放在那里还是应该把它的 body 和 Point[] 放在一起?body 可以有多个参数吗?我应该创建 File(fileName:string, points: Point[]) 并将其作为正文吗?

export class DataImportService {

constructor (private http: HttpClient){}

baseUrl: string="api/files";

postFile(fileName:string, points: Point[] ):Observable<any>{

return this.http.post(this.baseUrl,{fileName,points} ){};
}

对比

export class DataImportService {

constructor (private http: HttpClient){}

baseUrl: string="api/files";

postFile(file:File ):Observable<File>{

return this.http.post(this.baseUrl,file ){};
}

这是我的两个概念,我真的很想了解:

DataImportComponent{

constructor(private dataService: DataImportService)

saveFile(data.csv){
...
return this.dataService.dataService.post(...).subscribe(
x=>{parsed to json});

)
}
}

标签: angular-httpclient

解决方案


推荐阅读