1

I want to send an image to the server and upload it. I did this with the following code but it did not upload to the server. I have to send a parameter containing a photo with the name of "img" to the server. In Postman the upload operation is performed, but it is not uploaded in the flutter.

  Future<String> uploadImage(filename) async {
    var model=widget.parent.selectedJobModel;
    var request = http.MultipartRequest('POST', Uri.parse(baseUrl+"update_job"));
        request.headers.addAll({
      "key":reqKey,
      "token":widget.parent.widget.spParent.userLogin,
      "user_id":widget.parent.widget.spParent.userNo,
      "job_id":model.job_id,
    });
    request.files.add(await http.MultipartFile.fromPath('img', filename));
    var res = await request.send();
    return res.reasonPhrase;
  }
Akif
  • 7,098
  • 7
  • 27
  • 53
  • You can try to add the contentType parameter inside the fromPath method too: contentType: new MediaType('image', 'jpeg') – Akif Dec 06 '20 at 07:01
  • Akif ,How can I add it? – meisam emadian Dec 06 '20 at 07:03
  • request.files.add(await http.MultipartFile.fromPath('img', filename, contentType: new MediaType('image', 'jpeg'))); – Akif Dec 06 '20 at 07:05
  • error : the method MediaType isn't defined for the type PageState. – meisam emadian Dec 06 '20 at 07:12
  • Does this answer your question? [How to upload images and file to a server in Flutter?](https://stackoverflow.com/questions/49125191/how-to-upload-images-and-file-to-a-server-in-flutter) – towhid Dec 06 '20 at 07:49

0 Answers0