I'm trying to send an image to a server via multiPartRequest in flutter and when I add the image into the request files, once I want to specify the content type which is of MediaType, a compile-time error appear telling me that MediaType class is not defined.
How can I fix this problem?
http.MultipartRequest multipartRequest = new http.MultipartRequest('POST',url);
http.MultipartFile file = new http.MultipartFile.fromBytes('file', await
image.readAsBytes(),contentType: MediaType('image','jpg)); // MediaType class is not defined
multipartRequest.files.add(file);
you need to import:
import 'package:http_parser/http_parser.dart';
Just check out this if it works
uploadFile() async {
var postUri = Uri.parse("<APIUrl>");
var request = new http.MultipartRequest("POST", postUri);
request.fields['user'] = 'blah';
request.files.add(new http.MultipartFile.fromBytes('file', await File.fromUri("<path/to/file").readAsBytes(), contentType: new MediaType('image', 'jpeg')))
request.send().then((response) {
if (response.statusCode == 200) print("Uploaded!");
});
}
I figured out the answer and it is that I must import http parser package
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