gpt4 book ai didi

http - 通过http flutter上传图片时是否可以显示进度条?

转载 作者:行者123 更新时间:2023-12-04 13:33:48 30 4
gpt4 key购买 nike

我正在开发一个将图像上传到 的应用程序Amazon S3 存储桶 ,而不仅仅是显示 微调器 , 我很想得到 进度关于该上传的状态。
我正在使用 上传文件多部分请求 来自 包:http .我已成功上传文件,但我想获取 进度正在上传的文件。我怎样才能做到这一点?我当前的代码看起来像这样。

  static Future<bool> uploadFile({@required userId, @required albumId, @required data, @required fileName}) async{

const _accessKeyId = 'AKIAVV**********';
const _secretKeyId = 'iyBmdn6v***************************';
const _region = 'us-east-1';
const _s3Endpoint = 'https://myalbums-content.s3.amazonaws.com';

final file = data;
var stream = new http.ByteStream(DelegatingStream.typed(file.openRead()));
final length = await file.length();

final uri = Uri.parse(_s3Endpoint);
final req = http.MultipartRequest("POST", uri);

var multipartFile = http.MultipartFile('file', stream, length,
contentType: new MediaType('image','JPG'));

final policy = Policy.fromS3PresignedPost(userId.toString() +'/'+ albumId.toString() +'/'+ fileName,
'myalbums-content', _accessKeyId, 15, length, region: _region);

final key = SigV4.calculateSigningKey(_secretKeyId, 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;

try {
final res = await req.send();

if(res.statusCode == 204){
return true;
} else {
return false;
}
} catch (e) {
print('Network issue: '+e.toString());
return false;
}
}
先感谢您。

最佳答案

退房 this Github 上的例子。它显示了如何使用流上传文件。如果您公开上传到您的 Widget 的字节流,您应该能够使用 ProgressIndicator 将总字节数与流中的值进行比较。

static Future<String> fileUploadMultipart(
{File file, OnUploadProgressCallback onUploadProgress}) async {
assert(file != null);

final url = '$baseUrl/api/file';

final httpClient = getHttpClient();

final request = await httpClient.postUrl(Uri.parse(url));

int byteCount = 0;

var multipart = await http.MultipartFile.fromPath(fileUtil.basename(file.path), file.path);

// final fileStreamFile = file.openRead();

// var multipart = MultipartFile("file", fileStreamFile, file.lengthSync(),
// filename: fileUtil.basename(file.path));

var requestMultipart = http.MultipartRequest("", Uri.parse("uri"));

requestMultipart.files.add(multipart);

var msStream = requestMultipart.finalize();

var totalByteLength = requestMultipart.contentLength;

request.contentLength = totalByteLength;

request.headers.set(
HttpHeaders.contentTypeHeader, requestMultipart.headers[HttpHeaders.contentTypeHeader]);

Stream<List<int>> streamUpload = msStream.transform(
new StreamTransformer.fromHandlers(
handleData: (data, sink) {
sink.add(data);

byteCount += data.length;

if (onUploadProgress != null) {
onUploadProgress(byteCount, totalByteLength);
// CALL STATUS CALLBACK;
}
},
handleError: (error, stack, sink) {
throw error;
},
handleDone: (sink) {
sink.close();
// UPLOAD DONE;
},
),
);

await request.addStream(streamUpload);

final httpResponse = await request.close();
//
var statusCode = httpResponse.statusCode;

if (statusCode ~/ 100 != 2) {
throw Exception('Error uploading file, Status code: ${httpResponse.statusCode}');
} else {
return await readResponseAsString(httpResponse);
}
}
req.send还返回 StreamedResponse这可能更容易实现。

关于http - 通过http flutter上传图片时是否可以显示进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63409212/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com