将文本与其他值一起作为文件发送到后端

时间:2019-09-13 08:27:03

标签: angular typescript https

我要从文本字段中获取文本,并且需要使用backend 保存文本,后端不将数据另存为文本而是另存为文件,因此我需要将文本作为文件发送到后端

@Component({
    selector: 'app-x-page',
    templateUrl: './x-page.component.html',
    styleUrls: ['./x-page.component.scss']
})
export class XPageComponent{
    private xObj = {
        id: "ffff-ffff-ffff",
        title: "xxxxxx"
    }
    public xForm = new FormGroup({
      title: new FormControl('', Validators.required),
      file: new FormControl(''),
    })

    constructor(private XService: XService) { }

    public onSubmit(){
        this.xForm.get('file').setValue({
            filename: '_.md',
            type: 'text/plain',
            value: this.xForm.get('file').value
        });

        this.XService.updateX(
            this.xObj.id, 
            this.xObj.title, 
            this.xForm.value
        ).subscribe(data=>console.log(data));
    }


/* ... [XService] the updateX function ... */
public updateX(id: string, title: string, updateData){
    const url:string = `${this.appConfig.backendRoot}/X/${id}/${title}/`;
    return this.putDataToURL(url, updateData);
  }


/* .. [XService] pubDataToURL function ...*/
public putDataToURL<T>(url: string, data: any){
    /**
     * sending put request with the given data
     * to the given URL
     */
    return this.http.put(url, data, {
      headers: new HttpHeaders().set('X-CSRFToken', 
      this.userService.getCSRFToken())
    }).pipe(
          catchError(this.errorLogger(url))
    );
  }

发送时,我收到了不受支持的媒体类型415

0 个答案:

没有答案