admin管理员组文章数量:1390604
I have a Buffer that has the contains a PDF that I have not been able to successfully download. Every time I attempt to open the PDF, it fails to open. Here is the code I am using to download the file:
const content = new Blob(attach.content.data, { type: attach.contentType });
const encodedUri = window.URL.createObjectURL(content);
const link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", attach.filename);
link.click();
Here is what the attach object looks like:
Here is what the Blob looks like:
Halp, plx!!!1
I have a Buffer that has the contains a PDF that I have not been able to successfully download. Every time I attempt to open the PDF, it fails to open. Here is the code I am using to download the file:
const content = new Blob(attach.content.data, { type: attach.contentType });
const encodedUri = window.URL.createObjectURL(content);
const link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", attach.filename);
link.click();
Here is what the attach object looks like:
Here is what the Blob looks like:
Halp, plx!!!1
Share Improve this question edited Apr 3, 2021 at 16:18 Juliana Hill asked Apr 3, 2021 at 15:49 Juliana HillJuliana Hill 4404 silver badges15 bronze badges1 Answer
Reset to default 7The problem was that I was not converting the Node.js Buffer to a browser readable ArrayBuffer:
const data = Uint8Array.from(attach.content.data);
const content = new Blob([data.buffer], { type: attach.contentType });
const encodedUri = window.URL.createObjectURL(content);
const link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", attach.filename);
link.click();
本文标签: javascriptHow do I download a Buffer that is returned from my serverStack Overflow
版权声明:本文标题:javascript - How do I download a Buffer that is returned from my server? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744731693a2622085.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论