Not able to upload image to server using flutter API integration

Viewed 37

I am trying to send Form data to the backend using Flutter. As a result, all the text type data is sent easily but my image file is not shared. Help me Guys

uploadImage(
   filepath,url) async {
EasyLoading.show(status: 'Uploading Data...');
try {
  final result = await InternetAddress.lookup('example.com');
  if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
    var request = http.MultipartRequest('POST', Uri.parse(url));
    print(filepath); *// this filepath is not empty*
    request.files.add(await http.MultipartFile.fromPath('image', filepath));
    request.headers.addAll(headers);
    request.fields['name'] = _pName.text;
    request.fields['store_id'] = store_id;
    request.fields['seller_name'] = seller_name;
    request.fields['seller_id'] = seller_id;
    request.fields['product_id'] = 35.toString();
    request.fields['stock_status_id'] = 6.toString();
    request.fields['price'] = _pPrice.text;
    request.fields['model'] = _pModel.text;
    request.fields['sku'] = _pSKU.text;
    request.fields['status'] = 1.toString();
    request.fields['product_name'] = "Shoes Sport";
    request.fields['is_approved'] = 1.toString();
    request.fields['special'] = false.toString();
    request.fields['quantity'] = _pQuantity.text;
    var res = await request.send();
    if(res.statusCode==200)
    {
      EasyLoading.showSuccess('Data is Uploaded!\n Waiting For Approval');
      EasyLoading.dismiss();
      print(res.reasonPhrase);
      Navigator.pop(context);
    }
    print(request.fields);
    // print(filepath);

  }
  }
  on SocketException catch (_) {
  EasyLoading.showError("Internet Connection is not available");
  }
}

all the data is uploaded except image, i have also crosschecked the parameter its correct.

1 Answers

chek the spelling of "image" it should be same mention in your api doc and the one you are using, your code seems correct i was stuck at the same issue i was using the image key but the key was "profile " please do a recheck , let me know if this works

Related