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 ?
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With