Flutter使用http包监控FileUpload进度
Flutter Monitor FileUpload progress using the http package
我一直在使用以下代码在我的服务器上上传文件,因为它正在做这项工作,但我想在操作期间监控上传进度百分比并相应地更新 UI 以反映进度用户
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 获取最终的 return。
如何实现?
在 GitHub 上查看此 example。它演示了如何访问文件的当前上传进度。
我一直在使用以下代码在我的服务器上上传文件,因为它正在做这项工作,但我想在操作期间监控上传进度百分比并相应地更新 UI 以反映进度用户
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 获取最终的 return。
如何实现?
在 GitHub 上查看此 example。它演示了如何访问文件的当前上传进度。