通过相册选取照片上传,后端解析后是一张默认图。详见图片,
通过相册选取图片的uris 转化为FormData格式上传,转化的图片与实际上传图片不一致?
demo如下
try {
let photoSelectOptions = new picker.PhotoSelectOptions();
photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
photoSelectOptions.maxSelectNumber = 1;
let photoPicker = new picker.PhotoViewPicker();
photoPicker.select(photoSelectOptions, (err: BusinessError, photoSelectResult: picker.PhotoSelectResult) => {
if (err) {
console.error('PhotoViewPicker.select failed with err: ' + JSON.stringify(err));
return;
}
let uris = photoSelectResult.photoUris[0]
console.log('PhotoViewPicker.select success: ' + uris);
if (!uris) {
return
}
let file = fs.openSync(uris,fs.OpenMode.READ_ONLY);
let buffer = new ArrayBuffer(4096);
let readLen = fs.readSync(file.fd, buffer);
console.info('PhotoViewPicker data to file succeed and buffer size is:' + readLen);
fs.closeSync(file);
let formData = new FormData();
formData.append('file', buffer, 'upoad.jpg');
});
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
}
需要转换成internal://协议的url,例如:上传文件地址internal://cache/xx.jpg,这里对应的是沙箱目录haps/entry/cache下的xx.jpg
参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/app-file-upload-download-V5\#上传应用文件