7

I'm trying to upload a video file to my server using a post request.

var file = new File(videoPath);
var uri = Uri.parse(tokenizedUri);
HttpClientRequest request = await new HttpClient().postUrl(uri);

await request.addStream(file.openRead());
var response = await request.close();

response.transform(utf8.decoder).forEach((string) {
  print(string); // handle data
});

But the server doesn't get it. Why?

1
  • 1
    What is the question? Commented Dec 28, 2017 at 4:03

2 Answers 2

10

The correct way is to use a MultipartRequest:

    var uri = Uri.parse(url);
    var request = new MultipartRequest("POST", uri);

    var multipartFile = await MultipartFile.fromPath("package", videoPath);
    request.files.add(multipartFile);

    StreamedResponse response = await request.send();
    response.stream.transform(utf8.decoder).listen((value) {
      print(value);
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, I'm doing the same thing but not getting the file in server. Here is my code request.fields['title'] = title.text; request.fields['sub_title'] = subTitle.text; var profile_photo = await MultipartFile.fromPath("profile_photo", photo.path); request.files.add(profile_photo); request.files .add(await http.MultipartFile.fromPath('profile_video', video.path)); var response = await request.send(); var responseString = await response.stream.bytesToString(); print(responseString); {sub_title":"dd","profile_photo":{},"profile_video":{}}
1

You can use the Dio package. It supports large files and works fine with me.

sendFile(String kMainUrl, XFile file) async {
    String filePath = file.path;
    String fileName = 'any name';

    try {
      FormData formData = FormData.fromMap({
        "file":
        await MultipartFile.fromFile(filePath, filename:fileName),
      });
      Response response =
      await Dio().post(kMainUrl, data: formData);
      print("File upload response: $response");
      print(response.data['message']);
    } catch (e) {
      print("Exception Caught: $e");
    }
}

1 Comment

can you please tell me if the file size is more than 100 mb? how can we upload it in chunks using dio? please tell me if you know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.