在我的示例应用程序中,我允许用户传递一个URL,比如这个https://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?
发布于 2021-09-09 01:08:34
对于任何可能遇到这个问题的人,我能够确定如何解决这个问题。基本上,使用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
https://stackoverflow.com/questions/69098786
复制相似问题