[英]uploading a file via API, multipart/form-data
我在我的應用程序中使用 rembg: https://github.com/danielgatis/rembg
我想做什么使用 http.post 將圖像上傳到我的 API 端點;
運行這段代碼:
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))
})
讀取文件():
async readFile(path: string) {
const contents = await Filesystem.readFile({
path: path
});
return contents;
}
刪除背景():
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));
});
}
響應是:多部分中缺少邊界。
要求:
附加文件似乎有問題,對嗎? 如何從文件系統附加文件並通過 API 上傳此文件?
如果您使用@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
您需要獲取給定文件的數據,獲取 blob,然后設置 FormData。 這是 Ionic 的示例。
問候,弗洛
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.