Search code examples
flutterflutter-httpmultipartfile

Flutter - how to get Stream Response of Video mp4 and download it


i am using Multipart Request in which i post image after that i get response of video.mp4 but i dont know how to get this response i tried

       response.stream.bytetoString()  //which give me formatexception which is unexpected byte 

i dont know how to get this file and download and play it in my flutter app here is code:

   Uri uri = Uri.parse(HttpLinks.localUrl);

  var stream = ByteStream(image.openRead());
  stream.cast();
  var length = await image.length();

  // here is path of image is uploading

  var request = MultipartRequest('POST', uri)
    ..files.add(
     MultipartFile(
        'file1',
        stream,
        length,
        filename:image.path,
      // contentType: MediaType('image',"jpg/png"),
      ),
    );
  var response = await request.send();// here i get response 
  if (response.statusCode == 200) {
    print('quick response is ${await response.stream.bytesToString()}');

   // here is i get exception which is 
            FormatException: Unexpected extension byte (at offset 43)

    return await response.stream.bytesToString();
  } 

kindly guide me what should i do.


Solution

  • Thank you Rick for guide me about this issue! set this to get video.mp4 file using stream response

      if (response.statusCode == 200) {
    
     final directory = await getExternalStorageDirectory();
      var file = await File('${directory.path}/video.mp4').create(recrusive: true);
     var bytes = <int>[];
    response.Stream.listen{
      (newBytes){
        bytes.addAll(newBytes);
      },
       onDone: () async{
          await file.writeAsBytes(bytes);
       }
    }
    }
     return file;