http.StreamedResponse 内容长度 returns 空
http.StreamedResponse contentLength returns null
我正在使用 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
。
出于测试目的,我使用了 link 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}
有时服务器没有 return contentLength 。如果是这种情况,则 contentLength 将为空。
如果您可以控制服务器,就像您的情况一样,您可以在发送文件之前设置 x-decompressed-content-length header 文件大小。在客户端,您可以像这样检索 contentLength:
final contentLength = double.parse(response.headers['x-decompressed-content-length']);
如果您无法控制服务器,您可以只显示正在下载的字节数,或者如果您事先知道文件大小,您可以使用它来帮助计算下载的百分比下载的字节数。
已将 SetEnv no-gzip 1
添加到网络服务器中的 .htaccess
文件,它 returns content-length
我正在使用 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
。
出于测试目的,我使用了 link 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}
有时服务器没有 return contentLength 。如果是这种情况,则 contentLength 将为空。
如果您可以控制服务器,就像您的情况一样,您可以在发送文件之前设置 x-decompressed-content-length header 文件大小。在客户端,您可以像这样检索 contentLength:
final contentLength = double.parse(response.headers['x-decompressed-content-length']);
如果您无法控制服务器,您可以只显示正在下载的字节数,或者如果您事先知道文件大小,您可以使用它来帮助计算下载的百分比下载的字节数。
已将 SetEnv no-gzip 1
添加到网络服务器中的 .htaccess
文件,它 returns content-length