gpt4 book ai didi

file - Flutter 使用 http 包监控文件上传进度

转载 作者:行者123 更新时间:2023-12-03 21:15:08 30 4
gpt4 key购买 nike

我一直在使用以下代码在我的服务器上上传文件,因为它正在执行这项工作,但我想在操作期间监控上传进度百分比并相应地更新 UI 以向用户反射(reflect)进度

uploadFile({File imageFile, String refCode}) async {
// open a bytestream
var stream =
new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
// get file length
var length = await imageFile.length();

// string to uri
var uri = Uri.parse(
'http://-------------/api/FilesUploadB/?refCode=$refCode');

// create multipart request
var request = new http.MultipartRequest("POST", uri);

// multipart that takes file
var multipartFile = new http.MultipartFile('file', stream, length,
filename: basename(imageFile.path));

// add file to multipart
request.files.add(multipartFile);

// send
var response = await request.send();

// listen for response
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});

//return response.
}

请注意, value在监听中让我从服务器上的 WebAPI 获得最终返回。
如何做到这一点?

最佳答案

看看这个example在 GitHub 上。它演示了如何访问文件的当前上传进度。

关于file - Flutter 使用 http 包监控文件上传进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61383244/

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