Uploading a file to server using http MultiPartRequest

708 Views Asked by At

Hello to everyone i hope you all good.

Im trying to upload a image to a server im using this: But it dosent work. So please help me, i would really appreciate it.

  Future<int> uploadFile(imageFile) async {
    try {
      var request = http.MultipartRequest(
        'POST',
        Uri.parse('http://10.0.2.2:8000/objects/'),
      );
      Map<String, String> headers = {"Content-type": "multipart/form-data"};
      request.files.add(
        http.MultipartFile(
          'file',
          imageFile.readAsBytes().asStream(),
          imageFile.lengthSync(),
          contentType: MediaType(
            'image',
            'jpeg',
          ),
        ),
      );
      request.headers.addAll(headers);
      request.fields.addAll({
        "app_label": "files",
        "app_model": "file",
      });
      print("request: " + request.toString());
      var res = await request.send();
      print("This is response:" + res.toString());
      return res.statusCode;
    } catch (err) {
      print(err);
      return 502;
    }
  }

Response from the server and vscode: https://ibb.co/2SMTBxJ

You can check the project here: https://github.com/bRUNS123/hydra

Also if someone know how to put the image without crop like the "finished" file. I the app you can take an image from the gallery or camera, then you have a preview and you can send or save to the gallery. But i created an option to avoid the crop image, but when i try to pass the information to not crop, nothing happen.

Also i wanna know if i can put all my void functions in a separated file.

Hope you have a great day!!

0

There are 0 best solutions below