我不知道如何在Flutter中传输文件。

huangapple go评论91阅读模式
英文:

I don't know how to transfer files in flutter

问题

当您在Flutter中选择一张图片时,您会得到这些数据,我想将这些数据发送到Golang服务器,但我不知道如何做。我正在使用http包,而不是使用dio。

/private/var/mobile/Containers/Data/Application/EE757681-C1B8-4FCC-BAB2-9A685A29E4A5/tmp/image_picker_06D171DF-30FA-4C15-908E-7FE4AD379DD9-23404-000012F0F6E55F99.png

我甚至不知道如何从Golang获取文件并将其保存到S3,但首先我想能够将其从Flutter传输到Golang。

我该如何在Golang服务器上处理这个图片?请帮忙。

英文:

When I select a picture in flutter, I get this data, I want to send this data to the golang server, but I don't know how. I am using http package without using dio.

/private/var/mobile/Containers/Data/Application/EE757681-C1B8-4FCC-BAB2-9A685A29E4A5/tmp/image_picker_06D171DF-30FA-4C15-908E-7FE4AD379DD9-23404-000012F0F6E55F99.png

I don't even know how to get a file from golang and save it to s3, but first I want to be able to transfer it from flutter to golang.

How can I control the image on the golang server? Please help

答案1

得分: 1

你可以使用MultipartRequest类来实现这个目的,正如http包的文档中所说。

var uri = Uri.https('example.com', 'create');
var request = http.MultipartRequest('POST', uri)
  ..fields['user'] = 'nweiz@google.com'
  ..files.add(await http.MultipartFile.fromPath(
      'package', 'build/package.tar.gz',
      contentType: MediaType('application', 'x-tar')));
var response = await request.send();
if (response.statusCode == 200) print('Uploaded!');

MultipartRequest类

英文:

You can use MultipartRequest class to that purpose as it say in http package documentation

var uri = Uri.https('example.com', 'create');
var request = http.MultipartRequest('POST', uri)
  ..fields['user'] = 'nweiz@google.com'
  ..files.add(await http.MultipartFile.fromPath(
      'package', 'build/package.tar.gz',
      contentType: MediaType('application', 'x-tar')));
var response = await request.send();
if (response.statusCode == 200) print('Uploaded!');

MultipartRequest class

huangapple
  • 本文由 发表于 2021年9月14日 14:28:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/69172804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定