如何在Flutter中上传http.MultipartRequest请求上的文件时获取进度事件 [英] How to get progress event while uploading file on http.MultipartRequest request in flutter

查看:996
本文介绍了如何在Flutter中上传http.MultipartRequest请求上的文件时获取进度事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MultipartRequest package:http 上传文件。我已经成功上传了文件,但是我想获取正在上传的文件的进度。我该如何实现?我当前的代码如下所示:

I am uploading a file using MultipartRequest from package:http. I am successfully uploading the file but I want to get the progress of the file that is being uploaded. How can I achieve that? My current code looks something like this

Future submitFile(var report, File file) async {
var uri = Uri.parse(endpoint + "v1/reports");
  var request = http.MultipartRequest("POST", uri);
  await addHeaders(request.headers);
  request.fields.addAll(Report.toMap(report));
  if (file != null)
    request.files.add(await http.MultipartFile.fromPath(
      'report_resource',
      file.path,
    ));

  String response = "";
  await (await request.send()).stream.forEach((message) {
    response = response + String.fromCharCodes(message);
  });
  return response;
}

我搜索了解决方案,发现。而这篇帖子在某种程度上与我想要实现的目标,因为他正在使用其他客户端进行请求。

I searched for the solution, found this. And this post is somehow not similar to what I want to achieve, as he is using different client for the request.

也许我没有在正确的路径上进行搜索。感谢
的帮助。

Maybe I am not searching on the right path. Help is appreciated.

推荐答案

等了一周左右。我没有得到回应。因此,我自己开发了一个插件来实现此功能。包链接

After waiting for a week or so. I didn't get response. Thus I developed a plugin myself to get this behavior. Package link.

使用它的示例:

var request = MultipartRequest();

request.addFile("image", imagePath);

Response response = request.send();

response.onError = () {
  print("Error");
};

response.onComplete = (response) {
  print(response);
};

response.progress.listen((int progress) {
  print("progress from response object " + progress.toString());
});

更新2020年6月30日

软件包现在也支持iOS。

The package now supports iOS as well.

这篇关于如何在Flutter中上传http.MultipartRequest请求上的文件时获取进度事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆