Angular 6表单数据

时间:2018-10-23 13:20:30

标签: angular

我正在使用angular 6.我正在通过表单数据发送图像文件数据。但是我可以在API中获取文件数据。这是我的代码

 UpdateLogoprofile(data: any): Observable<any> {
    debugger;
    const model = {};
    const url = AppSettings.API_ENDPOINT + 'Client/UploadFileToStorage';
    console.log(data);
   // const body = JSON.stringify(data);
   //const body = data;
    const httpOptions = {
        headers: new HttpHeaders({
        'content-type': 'application/json',
        'Authorization': 'bearer ' + this.auth_token + ''
           })
         };
    return this.http.post(url, data, httpOptions)
        // .map(this.extractData)
        .map((response: Response) => <any>response.json())
        .catch(this.handleError);
    }

和FormData

 let formData: FormData = new FormData();
                     formData.append(file.name,file);
                    // formData.append('StudentId',this.StudentID);
                    debugger;
                    this._ClientService.UpdateLogoprofile(data).subscribe(
                        image => {

2 个答案:

答案 0 :(得分:0)

更改文件附加行

formData.append('key', file, file.name);

答案 1 :(得分:0)

请更正此行

this._ClientService.UpdateLogoprofile(data)

this._ClientService.UpdateLogoprofile(formData);

标题内容类型也应为“ content-type”:“ multipart / form-data”