颤振/飞镖:自动将上传到 S3 存储桶的图像设置为"图像/jpeg"?



每当我从Flutter应用程序将图像文件上传到我的bucket时,对象就会自动显示在元数据中,显示为";二进制/八位字节流";而不是";图像/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, length, 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';

最新更新