Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振/飞镖:自动将上传到S3 Bucket的图像设置为";图像/jpeg";?_Flutter_Dart_Amazon S3_Image Upload - Fatal编程技术网

Flutter 颤振/飞镖:自动将上传到S3 Bucket的图像设置为";图像/jpeg";?

Flutter 颤振/飞镖:自动将上传到S3 Bucket的图像设置为";图像/jpeg";?,flutter,dart,amazon-s3,image-upload,Flutter,Dart,Amazon S3,Image Upload,每当我从Flatter应用程序将图像文件上传到我的bucket时,该对象就会自动在元数据中显示为“二进制/八位字节流”,而不是我需要的“图像/jpeg”类别 我使用的代码如下所示 String filename}) async { final endpoint = 'https://$bucket.s3-$region.amazonaws.com '; final uploadDest = '$destDir/${filename ?? path.basename(file.pa

每当我从Flatter应用程序将图像文件上传到我的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'))
newhttp.MultipartFile.fromBytes('file',wait file.fromUri(“”.readAsBytes()),contentType:new-MediaType('image','jpeg'))

别忘了导入
import'包:http_parser/http_parser.dart'

该代码是否可以替代;http.MultipartFile('file',stream,length,filename:path.basename(file.path)?您只需要将它添加到您的Multipart
contentType:new MediaType('image','jpeg')
我尝试过这个;final MultipartFile=http.MultipartFile('file',stream length,filename:path.basename(file.path),contentType:new MediaType('image','jpeg'));但我遇到了这个错误;/C:/flatter/.pub cache/hosted/pub.dartlang.org/aws_s3_upload-1.1.2/lib/aws_s3_upload.dart:47:26:错误:找不到方法:“MediaType”。内容类型:new MediaType('image,'jpeg'))您需要导入http_parser import“package:http_parser/http_parser.dart”;这确实应该可以工作,并且在执行时没有显示错误,但不幸的是,上传的图像文件仍然在S3对象的元数据内容类型属性下注册为“二进制/八位字节流”。