How to upload images and file to a server in Flutter?

后端 未结 11 1079
攒了一身酷
攒了一身酷 2020-11-29 22:21

I use a web service for image processing , it works well in Postman:

Now I want to make http request in flutter with Dart:

import \'pa         


        
11条回答
  •  被撕碎了的回忆
    2020-11-29 23:00

    Your workaround should work; many servers will accept application/x-www-form-urlencoded as an alternative (although data is encoded moderately inefficiently).

    However, it is possible to use dart:http to do this. Instead of using http.post, you'll want to use a http.MultipartFile object.

    From the dart documentation:

    var request = new http.MultipartRequest("POST", url);
    request.fields['user'] = 'someone@somewhere.com';
    request.files.add(http.MultipartFile.fromPath(
        'package',
        'build/package.tar.gz',
        contentType: new MediaType('application', 'x-tar'),
    ));
    request.send().then((response) {
      if (response.statusCode == 200) print("Uploaded!");
    });
    

提交回复
热议问题