Home > Enterprise >  How to upload Multiple Images to rest API in Flutter?
How to upload Multiple Images to rest API in Flutter?

Time:11-17

I am trying to upload upload multiple images to Rest API in flutter. the code i have written is given below:

  final List<File> _image = [];
  Future<Future<bool?>?> uploadImage(filePath, url) async {

  if (_image.length > 0) {
  for (var i = 0; i < _image.length; i  ) {
    print(_image.length);
    var request =
        http.MultipartRequest('POST', Uri.parse(url   _scanQrCode));
    print(Uri.parse(url   _scanQrCode));
    request.files.add(http.MultipartFile.fromBytes(
      'picture',
      File(_image[i].path).readAsBytesSync(),
      filename: _image[i].path.split("/").last
    ));
    var res = await request.send();
      var responseData = await res.stream.toBytes();
      var result = String.fromCharCodes(responseData);
      print(_image[i].path);
  }

  _submitedSuccessfully(context);
}else{
  return Fluttertoast.showToast(
      msg: "Please Select atleast one image",
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.CENTER,
      timeInSecForIosWeb: 1,
      backgroundColor: Colors.red,
      textColor: Colors.white,
      fontSize: 16.0
  );
}
}

The code is not working, the image is not getting uploaded. Please anyone help me to solve this problem

CodePudding user response:

change your code to :

final List<File> _image = [];
Future<Future<bool?>?> uploadImage(String url) async {
     // create multipart request
     var request = http.MultipartRequest('POST', Uri.parse(url   _scanQrCode));
     
     
      if (_image.length > 0) {
        for (var i = 0; i < _image.length; i  ) {
          request.files.add(http.MultipartFile('picture',
          File(_image[i].path).readAsBytes().asStream(), File(_image[i].path).lengthSync(),
          filename: basename(_image[i].path.split("/").last)));
        }
        
        // send
        var response = await request.send();

      
        // listen for response
        response.stream.transform(utf8.decoder).listen((value) {
          debugPrint(value);
         _submitedSuccessfully(context);
       });
    }
    else{
  return Fluttertoast.showToast(
      msg: "Please Select atleast one image",
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.CENTER,
      timeInSecForIosWeb: 1,
      backgroundColor: Colors.red,
      textColor: Colors.white,
      fontSize: 16.0
     );
   }
}

CodePudding user response:

this package ease your work, flutter_uploader:

final uploader = FlutterUploader();

final taskId = await uploader.enqueue(
  url: "your upload link", //required: url to upload to
  files: [FileItem(filename: filename, savedDir: savedDir, fieldname:"file")], // required: list of files that you want to upload
  method: UploadMethod.POST, // HTTP method  (POST or PUT or PATCH)
  headers: {"apikey": "api_123456", "userkey": "userkey_123456"},
  data: {"name": "john"}, // any data you want to send in upload request
  showNotification: false, // send local notification (android only) for upload status
  tag: "upload 1"); // unique tag for upload task
);
  • Related