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

后端 未结 11 1015
攒了一身酷
攒了一身酷 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:43

    With dio I do like this:

    Future _uploadFileAsFormData(String path) async {
      try {
        final dio = Dio();
    
        dio.options.headers = {
          'Content-Type': 'application/x-www-form-urlencoded'
        };
    
        final file =
          await MultipartFile.fromFile(path, filename: 'test_file');
    
        final formData = FormData.fromMap({'file': file}); // 'file' - this is an api key, can be different
    
        final response = await dio.put( // or dio.post
          uploadFileUrlAsString,
          data: formData,
        );
      } catch (err) {
        print('uploading error: $err');
      }
    }
    

提交回复
热议问题