1

I am trying to upload an image using resigned URL .But I got Invalid argument(s): Invalid request body "[object File]".I tried with text also. I think issue is due to content type.But I don't know what content type is give for "Object File".

Future<void> uploadfile(String url, File image, String type) async {
   var urls = Uri.parse(url);
    try {
      try{
        var response = await http.put(
          urls,
          body:image,
          headers: {
            "Content-Type":"image/$type",
          },
        );
        print("respons is ${response.statusCode}");
      }catch(e){
        print("catch error is $e");
      }
    } catch (e) {
      throw ('Error uploading photo');
    }
  }
Developer
  • 148
  • 8
  • you cannot send a File type in body. convert it to bytes `Uint8List bytes = await image.readAsBytes();` and then use `body: bytes`. Let me know if it works – Ashutosh Patole Nov 10 '22 at 11:38
  • i got "The method 'readAsBytes' isn't defined for the type 'File' error when adding this Uint8List bytes = await image.readAsBytes() – Developer Nov 10 '22 at 11:49
  • sorry my bad. you have to convert it like this `var bytes = await File(image).readAsBytes();` assuming image is the imagePath – Ashutosh Patole Nov 10 '22 at 11:59
  • @AshutoshPatole Expected a value of type 'File', but got one of type 'File$' error is now showing. – Developer Nov 10 '22 at 12:16

1 Answers1

0

This is my function that i create it for update image, use this: and Uint8List is my type of image_file

        var request = http.MultipartRequest("put", uri);

          var image = http.MultipartFile.fromBytes(
            'image',
            image_file,
            filename: 'image.jpg',
          );
          request.files.add(image);
        
        request.fields.addAll(body as Map<String, String>);
        request.headers.addAll(headers!);
        response = await http.Response.fromStream(await request.send());
aminjafari-dev
  • 264
  • 1
  • 17