从 Slim v2.6.1 服务下载带有 Angular v1.5 的 pdf
Download pdf with Angular v1.5 from a Slim v2.6.1 service
我正在尝试从使用 Slim v2.6.1 开发的服务下载带有 Angular v1.5 的 pdf。
下载文件后打开没有内容,页面是空白的。
我不知道为什么源文件大小是87 KB,而下载文件是135 KB。
使用Slim框架开发的服务代码为:
$app->get('/pdfReport', function () use ($app) {
try {
$path = "./Service/reports/report_test.pdf";
$app->response->setStatus(200);
$app->response()->header('Content-Type','application/pdf' );
$app->response()->header('Content-Transfer-Encoding', 'Binary');
$app->response()->header('Content-disposition', 'attachment; filename="report_test"');
$app->response()->header('Content-Length', filesize($path));
$app->response()->header('Expires', '0');
$app->response()->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
$app->response()->header('Pragma', 'public');
ob_clean();
ob_start();
readfile($path);
$content = ob_get_clean();
$app->response()->body($content);
} catch (Exception $e) {
$arr = array('status' => "error", 'fault' => $e->getMessage());
}
});
在Angular中的代码是:
vm.generateReport = function() {
var uri = "api/pdfReport";
$http.get(uri, header).success(function(data) {
var data = new Blob([data], { type: "application/pdf" });
saveAs(data, 'Report.pdf');
});
}
下载PDF或图片等二进制数据时,设置responseType很重要:
vm.generateReport = function() {
var uri = "api/pdfReport";
var config = { responseType: 'blob' };
$http.get(uri, config).then(function(response) {
̶v̶a̶r̶ ̶d̶a̶t̶a̶ ̶=̶ ̶n̶e̶w̶ ̶B̶l̶o̶b̶(̶[̶d̶a̶t̶a̶]̶,̶ ̶{̶ ̶t̶y̶p̶e̶:̶ ̶"̶a̶p̶p̶l̶i̶c̶a̶t̶i̶o̶n̶/̶p̶d̶f̶"̶ ̶}̶)̶;̶
var data = response.data;
saveAs(data, 'Report.pdf');
});
}
有关详细信息,请参阅 MDN Web API Reference - XHR responseType
我正在尝试从使用 Slim v2.6.1 开发的服务下载带有 Angular v1.5 的 pdf。
下载文件后打开没有内容,页面是空白的。 我不知道为什么源文件大小是87 KB,而下载文件是135 KB。
使用Slim框架开发的服务代码为:
$app->get('/pdfReport', function () use ($app) {
try {
$path = "./Service/reports/report_test.pdf";
$app->response->setStatus(200);
$app->response()->header('Content-Type','application/pdf' );
$app->response()->header('Content-Transfer-Encoding', 'Binary');
$app->response()->header('Content-disposition', 'attachment; filename="report_test"');
$app->response()->header('Content-Length', filesize($path));
$app->response()->header('Expires', '0');
$app->response()->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
$app->response()->header('Pragma', 'public');
ob_clean();
ob_start();
readfile($path);
$content = ob_get_clean();
$app->response()->body($content);
} catch (Exception $e) {
$arr = array('status' => "error", 'fault' => $e->getMessage());
}
});
在Angular中的代码是:
vm.generateReport = function() {
var uri = "api/pdfReport";
$http.get(uri, header).success(function(data) {
var data = new Blob([data], { type: "application/pdf" });
saveAs(data, 'Report.pdf');
});
}
下载PDF或图片等二进制数据时,设置responseType很重要:
vm.generateReport = function() {
var uri = "api/pdfReport";
var config = { responseType: 'blob' };
$http.get(uri, config).then(function(response) {
̶v̶a̶r̶ ̶d̶a̶t̶a̶ ̶=̶ ̶n̶e̶w̶ ̶B̶l̶o̶b̶(̶[̶d̶a̶t̶a̶]̶,̶ ̶{̶ ̶t̶y̶p̶e̶:̶ ̶"̶a̶p̶p̶l̶i̶c̶a̶t̶i̶o̶n̶/̶p̶d̶f̶"̶ ̶}̶)̶;̶
var data = response.data;
saveAs(data, 'Report.pdf');
});
}
有关详细信息,请参阅 MDN Web API Reference - XHR responseType