英文:
Slim 4 + Angular 10 : donwload pdf api is returning empty data in angular blob response
问题
我正在尝试集成下载 PDF 的 Angular API 与 Slim 4 API。我可以在浏览器网络中看到来自 Slim 的响应,但当我尝试在 Angular 中解析响应时,它返回为空。我已在 Angular 和 Slim 的请求头中添加了必需的标头。
Angular 代码:
const blobHeaders = {
'Accept': 'application/pdf, application/json, text/plain, */*',
'Content-Type': 'application/json'
};
getInvoiceDetail(orderId, transId, customerId, brandId): Promise<any> {
return this.http
.get(`${this.url}/v1/orders/invoice/${orderId}/${transId}/${customerId}/${brandId}`, { headers: blobHeaders, responseType: 'blob', observe: 'response' })
.toPromise()
.then(response => {
console.log("---------main----------" + JSON.stringify(response))
return response;
})
.catch(this.commonService.handleError);
}
上面的 API 控制台返回如下内容:
---------main----------{"headers":{},"status":200,"statusText":"OK","url":"https://test.com/v1/orders/invoice/181/145/995/8","ok":true,"type":4,"body":{}}
Slim4 代码:
$response = new Response();
$csv_file = '/invoice/2023/06/14/2023FS00000165.pdf';
$response = $response
->withHeader('Content-Type', 'application/pdf')
->withHeader('Content-disposition', 'attachment; filename="test.pdf"')
->withHeader('Access-Control-Expose-Headers', '*')
->withHeader('Content-Description', 'File Transfer')
->withHeader('Content-Transfer-Encoding', 'binary')
->withHeader('Expires', '0')
->withAddedHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
->withHeader('Cache-Control', 'post-check=0, pre-check=0')
->withHeader('Pragma', 'no-cache')
->withBody((new \Slim\Psr7\Stream(fopen($csv_file, 'rb'))));
return $response;
从服务器获取到的 blob 响应如下:
我参考了许多帖子并发现需要在服务器端设置 'Access-Control-Expose-Headers'。我也已经这样做了,但仍然面临这个问题。
英文:
I am trying to integration download pdf angular api with slim 4 api. I can see the response returning from the slim on browser network, but when I try to parse the response in angular it is returning empty. I have added the required headers on both angular and slim.
Angular code:
const blobHeaders = {
'Accept' : 'application/pdf, application/json, text/plain, */*',
'Content-Type' : 'application/json'
};
getInvoiceDetail(orderId, transId, customerId, brandId) : Promise<any> {
return this.http
.get(`${this.url}/v1/orders/invoice/${orderId}/${transId}/${customerId}/${brandId}`, { headers: blobHeaders , responseType : 'blob', observe : 'response'})
.toPromise()
.then(response => {
console.log("---------main----------"+JSON.stringify(response))
return response;
})
.catch(this.commonService.handleError);
}
The above API console is returning the below:
---------main----------{"headers":{"normalizedNames":{},"lazyUpdate":null},"status":200,"statusText":"OK","url":"https://test.com/v1/orders/invoice/181/145/995/8","ok":true,"type":4,"body":{}}
Slim4 code:
$response = new Response();
$csv_file = '/invoice/2023/06/14/2023FS00000165.pdf';
$response = $response
->withHeader('Content-Type', 'application/pdf')
->withHeader('Content-disposition', 'attachment; filename="test.pdf"')
->withHeader('Access-Control-Expose-Headers', '*')
->withHeader('Content-Description', 'File Transfer')
->withHeader('Content-Transfer-Encoding', 'binary')
->withHeader('Expires', '0')
->withAddedHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
->withHeader('Cache-Control', 'post-check=0, pre-check=0')
->withHeader('Pragma', 'no-cache')
->withBody((new \Slim\Psr7\Stream(fopen($csv_file, 'rb'))));
return $response;
Getting this blob response from the server
I referred many posts and found need to set 'Access-Control-Expose-Headers' in server side. I did the same too, still facing the issue.
答案1
得分: 0
以下是已经翻译好的代码部分:
This is working after I changed the 'responseType : text' instead on blob.
getInvoiceDetail(orderId, transId, customerId, brandId) : Promise<any> {
return this.http
.get(`${this.url}/v1/orders/invoice/${orderId}/${transId}/${customerId}/${brandId}`, { headers: blobHeaders , responseType : 'text', observe : 'response'})
.toPromise()
.then(response => {
console.log("---------main----------"+JSON.stringify(response))
return response;
})
.catch(this.commonService.handleError);
}
英文:
This is working after I changed the 'responseType : text' instead on blob.
getInvoiceDetail(orderId, transId, customerId, brandId) : Promise<any> {
return this.http
.get(`${this.url}/v1/orders/invoice/${orderId}/${transId}/${customerId}/${brandId}`, { headers: blobHeaders , responseType : 'text', observe : 'response'})
.toPromise()
.then(response => {
console.log("---------main----------"+JSON.stringify(response))
return response;
})
.catch(this.commonService.handleError);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论