1

I am doing this:

 startUpload(File file) async {
    var bytes = await file.readAsBytes();
    var base64Img = base64.encode(bytes);
    debugPrint(base64Img);

    var apiUrl = "MY_URL";
    var requestBody = new Map<String, String>();
    requestBody["requestType"] = "upload";
    requestBody["job_id"] = jobId.toString();
    requestBody["picture"] = base64Img;
    http.Response response = await http.post(apiUrl, body: requestBody);
  }

And I noticed that somehow the length of base64Img received on the backend is smaller than that generated in the app. When I call base64Img.length I get 129648 but on the server, I get 66401. Please note that file is actually an image. So when I decode the one from the server, the lower part of the image doesn't display or is distorted somehow.

I am also sending this same data in native Android and there are no issues whatsoever.

So my question is, does dart has a max length limit on the values of request body data and how can I send large data?

X09
  • 3,827
  • 10
  • 47
  • 92

1 Answers1

2

To send large files across network I had to use MultipartRequest from http package, together with MultipartFile and it's constructor - MultipartFile.fromBytes in your case.

var url = Uri.parse("YOUR_URL");
var request = new http.MultipartRequest("POST", url);
request.fields['some_additional_field'] = 'other_field';
request.files.add(new http.MultipartFile.fromBytes("file", bytes, filename: 
file.name);
request.send().then((response) {
  if (response.statusCode == 200) print("Uploaded!");
});

Or you could use FormData to just send a file using FormData - eg. with HttpRequest from dart:html

void sendRequest(File file) {
  FormData data = new FormData();
  data.append('field', 'some_string');
  data.appendBlob('file', file);
  HttpRequest.request('YOUR_URL', method: "POST", sendData: data).then((req) 
  {
    print(req.responseText);
  });
}
Olaf Górski
  • 166
  • 5
  • Thanks for the answer. Please, note that the image needs to be sent as a base64 encoded string. Please is there any way I could do that with the first solution? For the second solution, I am finding it difficult importing `FormData`. – X09 May 15 '18 at 10:55
  • In that case try: https://stackoverflow.com/questions/47027418/how-to-send-image-through-post-using-json-in-flutter?rq=1 Also, FormData is in dart:html – Olaf Górski May 16 '18 at 11:54