0

在我的示例应用程序中,我让用户传递一个这样的 URLhttps://www.test.com?file:///path/filename.pdf

我正在提取该文件查询并将其传递到组件中:

<ngx-extended-pdf-viewer
  [src]="sourceUrl"
  [handTool]="false"
  [showFindButton]="true"
  zoom="100%"
  height="100vh"
  [useBrowserLocale]="true"
></ngx-extended-pdf-viewer>

我收到“消息:缺少 PDF”的错误。file:///有没有办法将本地路径转换为以 PDF开头的路径?

4

1 回答 1

1

对于任何可能偶然发现这个问题的人,我能够确定如何去做。基本上,使用 xhr 并将响应类型设置为arraybuffer

Idk,如果除了下面的代码之外我还需要做任何事情,但它可以工作。

  function downloadLocalFile(url: string, cb: (response: Uint8Array) => void) {
    var xhr = new XMLHttpRequest();
    xhr.onload = () => {
      const response = new Uint8Array(xhr.response);
      cb(response);
    };

    xhr.onerror = (error) => {
      console.log(error);
    };

    xhr.open("GET", decodeURIComponent(url));
    xhr.responseType = "arraybuffer";
    xhr.send();
  }

你可以像这样使用它:

downloadLocalFile('file:///path/file.pdf', (arrayBuffer) => {
  console.log(arrayBuffer);
});

我还没有尝试过fetch,我不太确定你是否可以将响应设置为arraybuffer

于 2021-09-09T01:08:34.293 回答