【问题标题】:MultipartRequest uploads successfully to s3, but data appears to be invalidMultipartRequest 成功上传到 s3,但数据似乎无效
【发布时间】:2020-11-11 22:14:33
【问题描述】:

这在 Flutter 中似乎是一个非常简单的过程。但是当我下载我上传到s3的视频时,我无法播放视频。视频播放器告诉我数据已损坏。 uploadUri 是一个预签名的 s3 链接。 filePath 是我的视频的正确路径。 我收到来自发送的 200 响应。

  Future<void> postVideo( String filePath, String uploadUri ) async {
    var request = http.MultipartRequest('PUT', Uri.parse( uploadUri ) );
    request.files.add(
        await http.MultipartFile.fromPath(
            'video',
            filePath,
            contentType: MediaType('video','mp4')
    ));
    final response = await request.send();
    if (response.statusCode == 201 || response.statusCode == 200 ) {
      print( 'submit video response: ' + response.toString() );
    } else {
      throw Exception('Failed to post video');
    }
  }

有什么想法吗?

【问题讨论】:

    标签: flutter http amazon-s3 multipart


    【解决方案1】:

    这是有效的,不知道是什么魔法..

    Future<void> postVideo( String filePath, String uploadUri ) async {
    File file = File(filePath);
    List<int> imageData = file.readAsBytesSync();
    
    var response = await http.put(uploadUri, body: imageData, headers: {
      "Content-Type": "octet-stream",
      "Content-Disposition": 'attachment; filename="resource"',
      "Content-Encoding": "identity",
      "Content-Length": file.lengthSync().toString()
    });
    
    if (response.statusCode == 201 || response.statusCode == 200 ) {
      print( 'submit video response: ' + response.toString() );
    } else {
      throw Exception('Failed to post story');
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2018-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多