简体   繁体   English

通过 API, multipart/form-data 上传文件

[英]uploading a file via API, multipart/form-data

I'm using rembg in my app: https://github.com/danielgatis/rembg我在我的应用程序中使用 rembg: https://github.com/danielgatis/rembg

What i want to do Upload a image to my API endpoint using http.post;我想做什么使用 http.post 将图像上传到我的 API 端点;

Running this code:运行这段代码:

this.photoService.readFile(this.publicImageUrl.uri).then(res => {

let data = res;
this.oryginalImg = data.data;

this.ApiService.removeBG(this.publicImageUrl.uri).then(res => console.log(res))

})

readFile():读取文件():

async readFile(path: string) {
  const contents = await Filesystem.readFile({
  path: path
});

return contents;
}

removeBG():删除背景():

  async removeBG(file: any) {

  let headers = { 'content-type': 'multipart/form-data' };
  let formData = new FormData();
  formData.append('file', file);

  let body = {
    'body': formData
   }


   let promise = new Promise<void>((resolve, reject) => {
      this.http.post(environment.photoServiceURL, body, { headers }).toPromise().then(res => { console.log(res); resolve(); }).catch(err => console.log(err));
});

  }

Response is: Missing boundary in multipart.响应是:多部分中缺少边界。

Request:要求:

It's look like there's something wrong with attaching a file, am I right?附加文件似乎有问题,对吗? How to attach file from filesystem and upload this file via API?如何从文件系统附加文件并通过 API 上传此文件?

If you using @capacitor/filesystem you need to to the follow:如果您使用@capacitor/filesystem,您需要执行以下操作:

const response = await fetch(file.data); // where file comes from Filesystem.readFile
const blob = await response.blob();
const formData = new FormData();
formData.append('file', blob, file.name);

// ...then upload your form as above

You need to fetch the data of your given file, get the blob and then set the FormData.您需要获取给定文件的数据,获取 blob,然后设置 FormData。 Here is a example from Ionic.是 Ionic 的示例。

Greetings, Flo问候,弗洛

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何发送多部分/表单数据文件? - How to send multipart/form-data file? 上传文件时,内容类型发送为“multipart/form-data”时找不到边界错误。如何解决此错误? - While uploading file getting error as boundary not found when content-type sent as “multipart/form-data”.How to solve this error? 在 angualr 中发送多部分/表单数据 POST API 请求 - sending multipart/form-data POST API requests in angualr Angular 7 Post 文件内容作为 multipart/form-data - Angular 7 Post file contents as multipart/form-data 使用 Content-Type multipart/form-data 上传 IFormFile 时缺少内容类型边界 - missing content-type boundary when uploading IFormFile with Content-Type multipart/form-data 将数据作为多部分/表单数据发送 - Send data as multipart/form-data 将多部分/表单数据发布到端点 - Post multipart/form-data to endpoint 如何从角度发布多部分/表单数据到Django REST API - How to post multipart/form-data from angular to Django REST API angular10 应用程序中多部分/表单数据的图像上传 API 错误 - Image upload API error for multipart/form-data in my angular10 application 使用ngx-mat-file-input从Angular上载文件作为多部分/表单数据 - Upload File as multipart/form-data from Angular with ngx-mat-file-input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM