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

后端 未结 11 1050
攒了一身酷
攒了一身酷 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 22:42

    There is a static method in MultipartFile class which will be helpful called, fromPath which returns Future. You can add the file in your request body using request.files.add() method.

    final postUri = Uri.parse(kAPIUrl);
    http.MultipartRequest request = http.MultipartRequest('POST', postUri);
    
    http.MultipartFile multipartFile =
    await http.MultipartFile.fromPath('image_file', filePath); //returns a Future
    
    request.files.add(multipartFile);
    
    http.StreamedResponse response = await request.send();
    
    

提交回复
热议问题