Ionic 如何使用CapacitorHttp下载PDF文件

9ceoxa92  于 2022-12-08  发布在  Ionic
关注(0)|答案(1)|浏览(252)

Do to issues with the server setting being controlled by another party who will not add (CORS) I am forced to use CapacitorHttp to access the file Url.
Here is what I have so far
ts

async downloadPDF( FullName: any, URL: any) {
    const options = {
      url: URL,
      headers: {
        'x-authorization-token': 'XXXXXXXXX',
        'access-control-allow-origin': 'https://XXXXXXXXXXXX/',
        'access-control-allow-methods': 'GET,POST,OPTIONS',
        'access-control-allow-headers': 'Content-Type, X-Authorization-Token, Origin',
      },
      params: { }, // can I add a version of responseType: 'blob' here
    };

  const response: HttpResponse = await CapacitorHttp.get(options);
  // Type 'HttpResponse' is missing the following properties from type 'Blob': size, type, arrayBuffer, slice, and 2 more.ts(2740)

  write_blob({
    directory: Directory.Documents,
    path: `${FILE_DIR}/${FullName}`,
    blob: response
  });
}

and the response is

response {"headers":{
  "Cache-Control":"no-cache,private",
  "Server":"nginx",
  "Etag":"\"632cc89b-1abb7\"","Last-Modified":"Thu,22 Sep 2022 20:42:03 GMT",
  "Content-Disposition":"inline; filename=\"sample upload.pdf\"",
  "Accept-Ranges":"bytes",
  "Content-Length":"109495",
  "Date":"Thu, 24 Nov 2022 16:15:26 GMT",
  "Content-Type":"application/pdf"},
  "url":"https://XXXXXXXXXX",
  "status":200
}

Is there a way I can add a version responseType: 'blob' to CapacitorHttp

mkshixfv

mkshixfv1#

结果发现CapacitorHttp会导致很多问题,对我自己来说,它甚至会干扰@angular/common/http,导致它无法工作。
https://github.com/capacitor-community/http

import { Http, HttpDownloadFileResult } from '@capacitor-community/http';
import { Filesystem, Directory } from '@capacitor/filesystem';

async downloadFile(name: any, URL: any) {
  const options = { url: URL, filePath: `${FILE_DIR}/${name}`, fileDirectory: Directory.Documents, method: 'GET', };
  const response: HttpDownloadFileResult = await Http.downloadFile(options);
}

在iOS和Android上下载并保存文件,没有CORS问题

相关问题