首页 > 解决方案 > 如何发送多部分/表单数据文件?

问题描述

我在 html 中有这个:

选择文件
  handleFileInput(files: FileList) {
    const headers = new Headers({ 'Content-Type': 'multipart/form-data' });
    var formData = new FormData();
    formData.append("myFile", files.item[0]);
    this.api.postWithoutEntity('/socialintegration/callback/attachment', { recipientId: this.selected.custSocId, file: formData }).subscribe();
    this.upload = false;
  }

但我得到了这个:当前请求不是多部分请求”

有什么建议吗?

标签: angular

解决方案


我不知道您的 this.api.postWithoutEntity 方法是如何工作的,但您没有使用 headers 变量。您必须根据要求传递您的标头

const headers = new Headers({ 'Content-Type': 'multipart/form-data' });

return this.http.post<any>(
  "/socialintegration/callback/attachment",
  {recipientId: this.selected.custSocId, file: formData},
   headers
);

https://angular.io/guide/http#updating-headers


推荐阅读