Problem with Uploading a video with MultiPart request (http package)

1,499 views
Skip to first unread message

Gabriele Mazzola

unread,
Aug 7, 2018, 2:14:41 PM8/7/18
to Flutter Dev
Hi everybody, I am experiencing problems in uploading a file to Vimeo using a streamed PUT request of the http package.

Steps:
  • Create multipartfile using http.MultipartFile.fromPath()
  • Add the newly created file to a http.MultipartRequest (PUT)
  • Add headers to the request (including the length of the multipart file)
  • Send the request
It seems there is a problem with the length of the file I'm trying to upload.

Useful lines of code:
Future<Null> uploadExerciseVideo(File video) async {


    final String uploadLinkSecure = "url of interest";

final videoUploadRequest =
http.MultipartRequest('PUT', Uri.parse(uploadLinkSecure));

var multipartFile =
await http.MultipartFile.fromPath("package", video.path);


videoUploadRequest.files.add(multipartFile); // add the file to the request

videoUploadRequest.headers["Authorization"] = "Bearer $vimeoAuthToken";
videoUploadRequest.headers["Accept"] = vimeoUploadHeader;
videoUploadRequest.headers["Content-Type"] = "video/mp4";
videoUploadRequest.headers["Content-Length"] = multipartFile.length.toString();


// Here I just tried different ways to get the length of the file. They all bring to the same result.
var length1 = await video.length();
var length2 = video.lengthSync();
var length3 = await video.readAsBytes().then((List bytes) => bytes.length);
var length4 = video.readAsBytesSync().length;
var length5 = await video.stat().then((FileStat stat) => stat.size);
var length6 = video.statSync();

print("$length1, $length2, $length3, $length4, ${multipartFile.length}, $length5, $length6");

final streamedResponse = await videoUploadRequest.send().then((value) => print(value)); //streamed response

}


As a result of this, I get the following message:

I/flutter (27116): 3302286, 3302286, 3302286, 3302286, 3302286, 3302286, FileStat: type file I/flutter (27116): changed 2018-08-07 20:01:17.000 I/flutter (27116): modified 2018-08-07 20:01:17.000 I/flutter (27116): accessed 2018-08-07 20:01:13.000 I/flutter (27116): mode rw-r----- I/flutter (27116): size 3302286 E/flutter (27116): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception: E/flutter (27116): Content size exceeds specified contentLength. 3302507 bytes written while expected 3302286. […)@uY³ªÆõ5ƒâåçõ8TrrÐô·eáÆÒ «y/]åhæù¬•2V7A‰ ]sŠÂÌô|Dl B žEc­ E/flutter (27116): ÔJõ(J1 ¥; X fEÏЋd@à3 ª¼ò7 ‚jï5:‰þ3<ÿ 9) r¥ À ¨ÏÇÚm>±%¸|0 º2õò DÌ¥Þ> (©¬í3¬  Œtð21H ÚíTâÓ¤¾u¥WÚ­Ù Ìø€D6tà M˜xG¿%Tù pßvü-V0K– w®Z`³» ÂñmŽG üµ%¼qH \ …àQ Tw¾d ùh Sú#í q7«Ðsw 

Please, let me know if you have a clue about this issue.
Thanks
 
Reply all
Reply to author
Forward
0 new messages