I am downloading a file from a server with a GET request. Like this.
import 'package:http/http.dart' as http;
final http.Response response = await http.get(uri, headers: headers);
if (response.isOk()) {
response.headers.forEach((key, value) {
print('key = $key; value = $value');
});
final Uint8List bytes = response.bodyBytes;
AnchorElement(href: "$contentPrefix${base64Encode(bytes)}")
..setAttribute("download", "file.xlsx")
..click();
} else {
print('report request status code is ${response.statusCode}');
}
I need to know the file name.
The file name is contained in the content-disposition
header, in different encodings. But it is not in http.Response.headers
.
How to extract filename in UTF-8
encoding?
2
Answers
Based on Richard Heap's comment.
In my case its Java server:
Content-Disposition
header:Im my case header value starts with:
attachment;filename="report.xlsx";filename*=utf-8''
.More info about filename in content disposition header.
Only certain HTTP response headers are available to cross site XHR requests, as would be the case in a Flutter Web application making a request from a server that didn’t launch it. Such a request firstly needs the relevant CORS headers, but if you want to be able to access one of the non-default response headers, also needs the
Access-Control-Expose-Headers
header.The CORS-safelisted response headers are:
Cache-Control
,Content-Language
,Content-Length
,Content-Type
,Expires
,Last-Modified
,Pragma
. SinceContent-Disposition
isn’t a safelisted header, your server will need to emit:The
Content-Disposition
header will then appear inresponse.headers
.