【发布时间】:2021-03-17 22:59:38
【问题描述】:
从昨天开始,我一直在努力尝试将使用移动相机从 iOS/Android 设备拍摄的图像上传到 azure blob 存储。
我可以上传文件,但由于某种原因它们已损坏无法打开上传的图像。
打开上传图片时请检查图片错误
我正在使用具有不同方法的 Flutter 包 http,所有这些都可以将图像文件上传到 azure blob 存储,但它会以某种方式损坏,我尝试将 ContentType 强制为 image/jpeg 但没有帮助。
这是我使用 http API 的代码 -
takePicture() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);
setState(() {
if (pickedFile != null) {
_image = File(pickedFile.path);
String fileName = basename(pickedFile.path);
uploadFile(fileName, image);
} else {
print('No image selected.');
}
});
}
第一种方法-->
http.Response response = await http.put(
uri,
headers: {
"Content-Type": 'image/jpeg',
"X-MS-BLOB-TYPE": "BlockBlob",
},
body: image.path,
);
print(response.statusCode);
使用第二个方法 -->
final data = image.readAsBytesSync();
var dio = Dio();
dio.options.headers['x-ms-blob-type'] = 'BlockBlob';
dio.options.headers['Content-Type'] = 'image/jpeg';
try {
final response = await dio.put(
'$url/$fileName?$token',
data: data,
onSendProgress: (int sent, int total) {
if (total != -1) {
print((sent / total * 100).toStringAsFixed(0) + "%");
}
},
);
print(response.statusCode);
} catch (e) {
print(e);
}
接近第三个 -->
var request = new http.MultipartRequest("PUT", postUri);
request.headers['X-MS-BLOB-TYPE'] = 'BlockBlob';
request.headers['Content-Type'] = 'image/jpeg';
request.files.add(
new http.MultipartFile.fromBytes(
'picture',
await image.readAsBytes(),
),
);
request.send().then((response) {
uploadResponse.add(response.statusCode);
}, onError: (err) {
print(err);
});
非常感谢您的帮助。
【问题讨论】:
-
我建议将
Dio()与此处列出的解决方案一起使用:stackoverflow.com/questions/59293114/…
标签: flutter dart azure-blob-storage