【发布时间】:2021-01-29 05:17:54
【问题描述】:
我正在尝试在 ionic Angular 应用程序中上传多个图像,但无法将正确的有效负载发送到 API。从邮递员那里发送数据运行良好。后端使用nodejs multer包处理图片上传。
如何将正确的负载从 UI 发送到后端。对于网络,我们可以使用表单数据上传图片,但移动端无法找到解决方案
在 HTML 文件中
<ion-button fill="outline" expand="block" color="secondary" (click)="selectImageSource()">
<ion-icon slot="start" name="camera"></ion-icon>
Upload
</ion-button>
TS 文件
async selectImageSource() {
const buttons = [
{
text: 'Choose from gallary',
icon: 'image',
handler: () => {
this.addImage(CameraSource.Photos);
}
}
];
const actionSheet = await this.actionSheetController.create({
header: 'Select image source',
buttons
});
await actionSheet.present();
}
async addImage(source: CameraSource) {
const image = await Camera.getPhoto({
quality: 100,
resultType: CameraResultType.Base64,
source,
allowEditing: false
});
const blobData = this.b64toBlob(image.base64String, `image/${image.format}`);
const uploadParams = {
userId: 'Abcd1234',
uploadType: 'profile-pics'
};
this.apiService.uploadPhotos(blobData, uploadParams)
.subscribe((img: any) => {
console.log('Image: ', img);
}, (err) => console.log('Eror: ', err));
}
b64toBlob(b64Data, contentType= '', sliceSize= 512) {
const byteCharacters = atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
const blob = new Blob(byteArrays, {type: contentType});
return blob;
}
【问题讨论】:
标签: angular ionic-framework multer-s3