现在的错误是API无法在响应中正确发送Content-Disposition。同时我不知道为什么。内容处置在下载功能中设置,例如:
if($file) {
//set headers, prevent chaching
header('Pragma: public');
header('Expires: -1');
header('Cache-Control: public, must-revalidate, post-check=0, pre-check=0');
//set appropriate headers for attachment or streamed file
if ($mode == 'attachment') {
header("Disposition: attachment; filename=\"{$file_name}.{$file_ext}\"");
}else
header("Content-Disposition: inline; filename=\"{$file_name}\"");
我在中间件之前的CORS配置是:
if ($app->request->getHeader('ORIGIN')) {
$origin = $app->request->getHeader('ORIGIN');
} else {
$origin = '*';
}
$app
->response
->setHeader('Access-Control-Allow-Origin', $origin)
->setHeader(
'Access-Control-Allow-Methods',
'GET,PUT,POST,DELETE,OPTIONS'
)
->setHeader(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Range, ' .
'Content-Disposition, Content-Type, Authorization'
)
->setHeader('Access-Control-Allow-Credentials', 'true');
$app->response->sendHeaders();
$referenceToken = $app->request->getHeader('Authorization');
if ($referenceToken != null)
$app->session->set("auth-token", $referenceToken);
return true;
}
我对中间件的回应:
$return = $app->getReturnedValue();
// $this->response->setContentType('text/plain', 'UTF-8');
if (is_array($return)) {
// Transforming arrays to JSON
// $app->response->setContent(json_encode($return));
} elseif (!strlen($return)) {
// Successful response without any content
$app->response->setStatusCode('204', 'No Content');
} else {
// Unexpected response
throw new Exception('Bad Response');
}
//Sending response to the client
$app->response->send();
return true;
这是我得到的输出:
前摄响应:
Request URL: http://localhost:5001/file/download/textjson.txt/1?responseType=text
Request Method: GET
Status Code: 200 OK
Remote Address: [::1]:5001
Referrer Policy: no-referrer-when-downgrade
响应标题:
Accept-Ranges: bytes
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Range, Content-Disposition, Content-Type, Authorization
Access-Control-Allow-Methods: GET,PUT,POST,DELETE,OPTIONS
Access-Control-Allow-Origin: http://localhost:4200
Cache-Control: public, must-revalidate, post-check=0, pre-check=0
Connection: Keep-Alive
Content-Encoding: gzip
Content-Type: text/plain;charset=UTF-8
Date: Wed, 29 Aug 2018 11:22:25 GMT
Disposition: attachment; filename="textjson.txt"
Expires: -1
Keep-Alive: timeout=5, max=99
Pragma: public
请求标头:
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
Authorization: 96675759dacbb51ade885b2be7d2665be79885148ca8e7beee27f3b12853073e
Connection: keep-alive
Host: localhost:5001
Origin: http://localhost:4200
Referer: http://localhost:4200/home
但此调用的角度输出:
const options = {
headers: new HttpHeaders().append('Authorization', this.oAuthService.getAccessToken()),
params: new HttpParams().append('responseType', 'text'),
observe: 'response' as 'response'
}
return this.http.get<any>(this.fileServerUrl + 'file/download/' + filename + '/' + version, options)
.subscribe(
resp => {
// display its headers
const keys = resp.headers.keys();
this.headers = keys.map(key =>
`${key}: ${resp.headers.get(key)}`);
console.log(this.headers);
});
是:
(4) ["pragma: public", "content-type: text/plain;charset=UTF-8", "cache-control: public, must-revalidate, post-check=0, pre-check=0", "expires: -1"]
所以在Response标头中有一个Disposition标头,但是为什么他不在标头数组中?