最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I download a Buffer that is returned from my server? - Stack Overflow

programmeradmin2浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 7

The 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();
发布评论

评论列表(0)

  1. 暂无评论