Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload images to server in Flutter?

I would like to upload a image, I am using http.Client() for making requests,

static uploadImage(String id, File file) {   var httpClient = createHttpClient();    Map<String, String> headers = new Map<String, String>();   headers.putIfAbsent("Authorization", () => "---");   headers.putIfAbsent("Content-Type", () => "application/json");    var body=new List();   body.add(id.)   httpClient.post(URL_UPLOADIMAGE,headers: headers,body: ,encoding: ) } 

What should be the body and encoding part for the request ?

like image 210
karan vs Avatar asked Jun 30 '17 08:06

karan vs


People also ask

How do you upload a file to the server in Flutter?

Server Side PHP Code: test/file_upload.php Now flutter part, Add the file_picker, path, and dio Flutter package in your project by adding the following line in pubspec. yaml file. In this way, you can upload file from flutter.


1 Answers

Use MultipartRequest class

Upload(File imageFile) async {         var stream = new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));       var length = await imageFile.length();        var uri = Uri.parse(uploadURL);       var request = new http.MultipartRequest("POST", uri);       var multipartFile = new http.MultipartFile('file', stream, length,           filename: basename(imageFile.path));           //contentType: new MediaType('image', 'png'));        request.files.add(multipartFile);       var response = await request.send();       print(response.statusCode);       response.stream.transform(utf8.decoder).listen((value) {         print(value);       });     } 

name spaces:

import 'package:path/path.dart'; import 'package:async/async.dart'; import 'dart:io'; import 'package:http/http.dart' as http; 
like image 188
Shyju M Avatar answered Sep 19 '22 02:09

Shyju M