Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

upload image with Chopper

How to upload an image to the server with chopper library? I tried the search on google but, I couldn't manage to get one.

What I have tried is

Function handle creating the chopper client

static ApiService create(String accessToken) {
final client = ChopperClient(
    baseUrl: 'http://192.168.43.125:8000/api',
    services: [
      _$ApiService(),
    ],
    converter: JsonConverter(),
    interceptors: [
      HeadersInterceptor({
        "Content-Type": "application/x-www-form-urlencoded",
        'Authorization': 'Bearer $accessToken',
        'Accept': 'application/json',
      }),
      HttpLoggingInterceptor()
    ]);
return _$ApiService(client);} 

API

@Post(path: '/inspectionStore',)
@multipart
Future<Response> storeInspection(
@Part("field") String field, @PartFile("file") http.MultipartFile file);

Code that do the work

File actualFile = photoSection.postImage[0];
http.MultipartFile file = await http.MultipartFile.fromPath('file', actualFile.path,contentType: MediaType('image', 'jpg'));

var response = await Provider.of<ApiService>(context).storeInspection('some name',file);

This is what the server retrieve (Laravel Log file) enter image description here

How can I get a proper data that can be used?

like image 792
O.Chounry Avatar asked Nov 21 '25 21:11

O.Chounry


1 Answers

I know it's a little late but this could save someone. So, you need to provide the path to your image as string

@multipart
Future<Response> storeInspection(
@Part("field") String field, @PartFile("file") String file);

then

File actualFile = photoSection.postImage[0];

var response = await Provider.of<ApiService>(context).storeInspection('some name',file.path);
like image 99
Dennis Mwea Avatar answered Nov 23 '25 11:11

Dennis Mwea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!