Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter DIO: upload image using binary body with Dio package

With thwe http package I can send an image to a server by putting te binary data in the body of a post call like in the snippet of this code:

var response = await http.post('My_url', body: File(path).readAsBytesSync(),  headers: {
                    'apikey': 'myAPIKEY',
                    'Content-Type': 'image/*', // set content-length
                  });

I can't do the same thing by using Dio, I don't know how to put directly the binary data in the body (like i can do it with postman)

postman

like image 321
Jaeger Avatar asked Jun 30 '20 01:06

Jaeger


People also ask

Is it possible to upload a byte array to a Dio?

DIO did do the upload but only pushed the byte array, which was not helpful. Thanks! – ChillBroDev Mar 25 at 15:29 Add a comment | 3 I also faced the same you have. In DIO you have to send the binary data through streams. Here is the example how I achieved it,

How to upload a file in flutter?

Generally the file upload is done in two ways. File will be a part form request along with other key-value data. flutter image upload with form data File itself is the body of post/put request.

What happened to uploadfileinfomethod in Dio?

In Dio latest version, UploadFileInfomethod has been replaced by MultipartFileclass. And here the way how to use to post image, video or any file:

How do I use Dio in YAML?

1. Add Dio package This will add a line like this to your package’s pubspec.yaml (and run an implicit dart pub get ): First, create an UserModel class that contains the data from the network request. It includes a factory constructor that creates an UserModel from JSON.


2 Answers

I also faced the same you have. In DIO you have to send the binary data through streams. Here is the example how I achieved it,

Uint8List image = File(path).readAsBytesSync();

Options options = Options(
  contentType: lookupMimeType(path),
  headers: {
    'Accept': "*/*",
    'Content-Length': image.length,
    'Connection': 'keep-alive',
    'User-Agent': 'ClinicPlush'
  }
);

Response response = await dio.put(
  url,
  data: Stream.fromIterable(image.map((e) => [e])),
  options: options
);
like image 172
Anirban Das Avatar answered Oct 22 '22 13:10

Anirban Das


Just putting my solution if someone stumbles upon the same issue.

I had to upload the file at a signed google storage URL. API required to insert the file binary data in the body of the PUT request. Couldn't implement using the DIO plugin, I resolved the issue using the DART HTTP package, Below is a sample code.

import 'package:http/http.dart' as http;

await http.put(
  Uri.parse(uploadURL),
  headers: {
    'Content-Type': mimeType,
    'Accept': "*/*",
    'Content-Length': File(filePath).lengthSync().toString(),
    'Connection': 'keep-alive',
  },
  body: File(filePath).readAsBytesSync(),
);
like image 4
Mehul Prajapati Avatar answered Oct 22 '22 13:10

Mehul Prajapati