Slim 4 + Angular 10:下载PDF API 在 Angular Blob 响应中返回空数据

huangapple go评论78阅读模式
英文:

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 响应如下:

Slim 4 + Angular 10:下载PDF API 在 Angular 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 = { 
&#39;Accept&#39; : &#39;application/pdf, application/json, text/plain, */*&#39;,
&#39;Content-Type&#39; : &#39;application/json&#39;
};

getInvoiceDetail(orderId, transId, customerId, brandId) : Promise&lt;any&gt; {
    return this.http
      .get(`${this.url}/v1/orders/invoice/${orderId}/${transId}/${customerId}/${brandId}`, { headers: blobHeaders , responseType : &#39;blob&#39;, observe : &#39;response&#39;})
      .toPromise()
      .then(response =&gt; {
        console.log(&quot;---------main----------&quot;+JSON.stringify(response))
        return response;
      })
      .catch(this.commonService.handleError);
  }

The above API console is returning the below:

---------main----------{&quot;headers&quot;:{&quot;normalizedNames&quot;:{},&quot;lazyUpdate&quot;:null},&quot;status&quot;:200,&quot;statusText&quot;:&quot;OK&quot;,&quot;url&quot;:&quot;https://test.com/v1/orders/invoice/181/145/995/8&quot;,&quot;ok&quot;:true,&quot;type&quot;:4,&quot;body&quot;:{}}

Slim4 code:

$response = new Response();			
        $csv_file = &#39;/invoice/2023/06/14/2023FS00000165.pdf&#39;;

        $response = $response
            -&gt;withHeader(&#39;Content-Type&#39;, &#39;application/pdf&#39;)
            -&gt;withHeader(&#39;Content-disposition&#39;, &#39;attachment; filename=&quot;test.pdf&quot;&#39;)
            -&gt;withHeader(&#39;Access-Control-Expose-Headers&#39;, &#39;*&#39;)
            -&gt;withHeader(&#39;Content-Description&#39;, &#39;File Transfer&#39;)
            -&gt;withHeader(&#39;Content-Transfer-Encoding&#39;, &#39;binary&#39;)
            -&gt;withHeader(&#39;Expires&#39;, &#39;0&#39;)
            -&gt;withAddedHeader(&#39;Cache-Control&#39;, &#39;must-revalidate, post-check=0, pre-check=0&#39;)
            -&gt;withHeader(&#39;Cache-Control&#39;, &#39;post-check=0, pre-check=0&#39;)
            -&gt;withHeader(&#39;Pragma&#39;, &#39;no-cache&#39;)
            -&gt;withBody((new \Slim\Psr7\Stream(fopen($csv_file, &#39;rb&#39;))));

        return $response;

Getting this blob response from the server
Slim 4 + Angular 10:下载PDF API 在 Angular Blob 响应中返回空数据

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 : &#39;text&#39;, observe : &#39;response&#39;})
      .toPromise()
      .then(response =&gt; {
        console.log(&quot;---------main----------&quot;+JSON.stringify(response))
        return response;
      })
      .catch(this.commonService.handleError);
  }

huangapple
  • 本文由 发表于 2023年6月15日 16:41:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定