Flutter: upload image using binary body

4,123

The body parameter of the put method accepts a List<int> that will be used as a list of bytes

From the http API reference: https://pub.dev/documentation/http/latest/http/put.html

body sets the body of the request. It can be a String, a List or a Map. If it's a String, it's encoded using encoding and used as the body of the request. The content-type of the request will default to "text/plain".

If body is a List, it's used as a list of bytes for the body of the request.

If body is a Map, it's encoded as form fields using encoding. The content-type of the request will be set to "application/x-www-form-urlencoded"; this cannot be overridden.

Examples to send a file:

main() async {
  await put(url, body: File('the_file').readAsBytesSync());
}
Share:
4,123
Little Monkey
Author by

Little Monkey

I'm just a little monkey!

Updated on December 11, 2022

Comments

  • Little Monkey
    Little Monkey over 1 year

    I want to upload a file using binary body like in the screenshot:

    screenshot

    So far I just have:

          save() async {
             http.put(url,headers:headers, body: );
    
  • Elia Weiss
    Elia Weiss over 4 years
    What is DelegatingStream?
  • Deepak gupta
    Deepak gupta almost 3 years
    Worked for me. I was uploading the file on AWS3 bucket and not able to send file over the server but this solution helped me lot. Thanks @Xavier.
  • M.SH
    M.SH almost 2 years
    Worked well :+1