如何解决Flutter/Dart:自动将上传到 S3 Bucket 的图像设置为“图像/jpeg”?
每当我从 Flutter 应用程序将图像文件上传到我的存储桶时,该对象会自动在元数据中显示为“二进制/八位字节流”,而不是我需要的“图像/jpeg”类别。
我使用的代码是这样的;
String filename}) async {
final endpoint = 'https://$bucket.s3-$region.amazonaws.com ';
final uploadDest = '$destDir/${filename ?? path.basename(file.path)}';
final stream = http.ByteStream(Stream.castFrom(file.openRead()));
final length = await file.length();
final uri = Uri.parse(endpoint);
final req = http.MultipartRequest("POST",uri);
final multipartFile = http.MultipartFile('file',stream,length,filename: path.basename(file.path)
);
final policy = Policy.fromS3PresignedPost(uploadDest,bucket,accessKey,15,region: region);
final key = SigV4.calculateSigningKey(secretKey,policy.datetime,region,'s3');
final signature = SigV4.calculateSignature(key,policy.encode());
req.files.add(multipartFile);
req.fields['key'] = policy.key;
req.fields['acl'] = 'public-read';
req.fields['X-Amz-Credential'] = policy.credential;
req.fields['X-Amz-Algorithm'] = 'AWS4-HMAC-SHA256';
req.fields['X-Amz-Date'] = policy.datetime;
req.fields['Policy'] = policy.encode();
req.fields['X-Amz-Signature'] = signature;
如何让照片自动上传为“图像/jpeg”,以便在浏览器中查看而不是下载到我的桌面?
解决方法
您可以附加媒体 contentType: new MediaType('image','jpeg')
类型,如下所示
new http.MultipartFile.fromBytes('file',await File.fromUri("<path/to/file>").readAsBytes(),contentType: new MediaType('image','jpeg'))
不要忘记导入 import 'package:http_parser/http_parser.dart';
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。