简体   繁体   English

从数组创建pdf Blob并查看

[英]Creating pdf Blob from array and viewing it

I'm having a trouble with getting my pdf to show up. 我在显示pdf时遇到麻烦。 I get the file data as byte array. 我得到的文件数据为字节数组。 This is what I have at the moment: 这是我目前所拥有的:

    // data is originally String {0: % 1:P 2:D ...}
    const byteArray = _.map(data); // ["%", "P", "D", "F", "-", "1", ".", "4", "↵", ...]
    const blob = new Blob(byteArray, {type: 'application/pdf'});
    const blobURL = URL.createObjectURL(blob);
    window.open(blobURL),

Blob shows up when I log it and it has proper length and all. 当我记录它时,Blob出现,并且它具有适当的长度和全部。 For some reason the pdf opens but is empty. 出于某些原因,pdf打开,但为空。 Only the header in the tab is right (so there must be something right?). 选项卡中只有标题是正确的(因此必须有正确的内容吗?)。 There is a possibility that the fault is in the backend where I get the data from but I'm not sure as I don't have access to it. 故障可能出在我从那里获取数据的后端,但是我不确定,因为我无法访问它。 You may also suggest better formats to transfer the data from backend if that is necessary. 如果必要的话,您也可以建议使用更好的格式从后端传输数据。

Could the data be presented wrong in it's original form? 数据以原始格式显示错误吗?

Edit: 编辑:

When I do the request with postman, it displays the file correctly without me doing anything 当我向邮递员发出请求时,它无需任何操作即可正确显示文件

I solved the problem by adding responseType='arraybuffer' to the request config. 我通过将responseType ='arraybuffer'添加到请求配置中解决了该问题。 After that this worked well. 在那之后,这很好。

const blob = new Blob([byteArray], {type: 'application/pdf'});
const blobURL = URL.createObjectURL(blob);
window.open(blobURL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM