【问题标题】:http.StreamedResponse contentLength returns nullhttp.StreamedResponse contentLength 返回 null
【发布时间】:2020-08-18 07:32:28
【问题描述】:

我正在使用flutter http https://pub.dev/packages/http 包从我自己的网络服务器下载文件。

下面是下载功能:

  download(String url, {String filename}) async {
    var client = http.Client();
    var request = new http.Request('GET', Uri.parse(url));
    var response = client.send(request);
    String dir = (await getApplicationDocumentsDirectory()).path;

    List<List<int>> chunks = new List();
    int downloaded = 0;

    response.asStream().listen((http.StreamedResponse r) {
      r.stream.listen((List<int> chunk) {
        // Display percentage of completion
        debugPrint('downloadPercentage: ${downloaded / r.contentLength * 100}');

        chunks.add(chunk);
        downloaded += chunk.length;
      }, onDone: () async {
        // Display percentage of completion
        debugPrint('downloadPercentage: ${downloaded / r.contentLength * 100}');

        // Save the file
        File file = new File('$dir/$filename');
        final Uint8List bytes = Uint8List(r.contentLength);
        int offset = 0;
        for (List<int> chunk in chunks) {
          bytes.setRange(offset, offset + chunk.length, chunk);
          offset += chunk.length;
        }
        await file.writeAsBytes(bytes);
        return;
      });
    });
  }

但是当我从我的网络服务器下载文件时,StreamedResponse r 始终是 null。我可以从其他网络服务器下载文件,它不是null

出于测试目的,我使用了链接https://www.rarlab.com/rar/wrar590.exe

因此,我认为问题可能是因为我的网络服务器上的设置。

来自 WinRAR 服务器

I/flutter (18386): IOStreamedResponse
I/flutter (18386): ResponseCode: 200
I/flutter (18386): {last-modified: Thu, 26 Mar 2020 10:03:25 GMT, date: Mon, 04 May 2020 11:47:34 GMT, accept-ranges: bytes, strict-transport-security: max-age=31536000;, content-length: 3007728, etag: "2de4f0-5a1bf18799540", content-type: application/octet-stream, server: Apache}

来自我的网络服务器:

I/flutter (29858): IOStreamedResponse
I/flutter (29858): ResponseCode: 200
I/flutter (29858): {connection: keep-alive, last-modified: Thu, 16 Apr 2020 20:22:20 GMT, set-cookie: __cfduid=d2789e7dce3f93d32c76e1d7874ffce1b1588599373; expires=Wed, 03-Jun-20 13:36:13 GMT; path=/; domain=.devoup.com; HttpOnly; SameSite=Lax, cf-request-id: 02817fd5a30000ddc752a1d200000001, transfer-encoding: chunked, date: Mon, 04 May 2020 13:36:13 GMT, access-control-allow-origin: *, vary: Accept-Encoding,User-Agent, content-encoding: gzip, cf-cache-status: DYNAMIC, content-type: application/zip, server: cloudflare, accept-ranges: bytes, cf-ray: 58e29c029952ddc7-SIN}

【问题讨论】:

  • 您需要response.asStream().listen 做什么?你有var response = client.send(request); - 这里var responseFuture&lt;StreamedResponse&gt; 所以只需等待Future 得到StreamedResponse
  • @pskink 因为我想显示下载百分比。
  • var response = await client.send(request); print(response.runtimeType); print(response.statusCode); print(response.headers); response.stream.listen((data) { print('chunk: ${data.length}'); });如果你使用这个,你会看到什么?
  • 所以你可以看到你根本不需要response.asStream().listen(...)
  • @pskink 状态码是404。当我使用你的代码时,它只打印收到的第一个块。我还想获得下载文件的文件大小。这样只有我才能显示进度。

标签: .htaccess flutter dart webserver cpanel


【解决方案1】:

有时服务器不返回 contentLength 。如果是这种情况,那么 contentLength 将为空。

如果您可以控制服务器,就像您的情况一样,您可以在发送之前将 x-decompressed-content-length 标头设置为文件大小。在客户端,您可以像这样检索 contentLength:

final contentLength = double.parse(response.headers['x-decompressed-content-length']);

如果您无法控制服务器,则可以只显示正在下载的字节数,或者如果您事先知道文件大小,则可以使用它来帮助计算下载字节数的百分比.

【讨论】:

    【解决方案2】:

    在网络服务器中将SetEnv no-gzip 1 添加到.htaccess 文件,它返回content-length

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 2020-06-19
      相关资源
      最近更新 更多