const arraybuffer = new Int8Array(buffer.data);

ArrayBuffer  -> Blob

const blob = new Blob([arraybuffer], { type : 'application/pdf'});

项目案例: 前端通过 ajax(responseType = 'blob')获取文件数据进行下载。代码如下,downloadFromS3 是共通方法通过 AWS S3 获取文件,因为返回的是 nodejs 的 Buffer 类型。前端 ajax 会自动转换为 blob 类型,所以没有问题。

但是现在后端可能会报 error ,想让前端截获 error ,这时前端会将 error 信息转换成 blob。如果读取 blob 信息也可以获取 error 信息,但是有没有其它方法,比如 responseType = 'json' 就可以正常读到 error 信息,但是前端怎么将 buffer 转换为 blob 呢?

后端 express.js

app.get('/', async (req, res) => {
  const pdf = await downloadFromS3("pdf/胡雨南的简历.pdf");
  res.send(pdf);

前端 index.js

const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
  const url = URL.createObjectURL(xhttp.response);
  const aTag = document.createElement('a');
  aTag.setAttribute('download', 'test');
  aTag.href = url;
  aTag.click();
xhttp.onerror = function() {
  console.log(xhttp.response);
xhttp.open('GET', 'http://localhost:3000', true);
xhttp.responseType = 'blob';
xhttp.send();

更改后端 express.js

app.get('/', async (req, res) => {
  const pdf = await downloadFromS3("pdf/胡雨南的简历.pdf");
  res.send({pdf : pdf});

更改前端 index.js

const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
  const buffer = xhttp.response.pdf;
  const dataView = new Uint8Array(buffer.data);
  const blob = new Blob([dataView], { type : 'application/pdf'});
  const url = URL.createObjectURL(blob);
  const aTag = document.createElement('a');
  aTag.setAttribute('download', 'test');
  aTag.href = url;
  aTag.click();
xhttp.onerror = function() {
  console.log(xhttp.response);
xhttp.open('GET', 'http://localhost:3000', true);
xhttp.responseType = 'blob';
xhttp.send();
Buffer -> ArrayBufferconst arraybuffer = new Int8Array(buffer.data);ArrayBuffer -> Blobconst blob = new Blob([arraybuffer], { type : 'application/pdf'});项目案例:前端通过 ajax(responseType='blob')获取文件数据进行下载。代码如下,downloadFromS3 是共通方法通过 AWS S3 获取... npm install buffer-to-arraybuffer var bufferToArrayBuffer = require ( 'buffer-to-arraybuffer' ) ; var b = new Buffer ( 12 ) ; b . write ( 'abc' , 0 ) ; var ab = bufferToArrayBuffer ( b ) ; String . fromCharCode . apply ( null , new Uint8Array ( ab ) ) ; // 'abc' 注意:如果仅将节点v4.3+目标,则只需执行以下操作: new Buffer ( [ 12 ] ) . buffer 麻省理工学院 responseType 可选的参数有:"text"、"arraybuffer"、"blob" 或 "document"; 对应的返回数据为 DOMString、ArrayBufferBlob、Document;默认参数为"text"。 前端请求二进制数据的时候需要设置数据响应格式:xhr.responseType = "arraybuffer"; 写法如下: 2.base64 转成blob 上传 function dataURItoBlob(dataURI) { var byteString = atob(dataURI.split(',')[1]); var mimeString = dataURI.split(
在Node中,应用需要处理网络协议、操作数据库、处理图片、接收上传文件等,在网络流和文件的操作中,还要处理大量二进制数据,Buffer类被引入作为NodejsAPI的一部分,使其可以在TCP流和文件系统操作等场景中处理二进制数据流。 Buffer是一个典型的JavaScript与C++结合的模块,它将性能相关部分用C++实现,将非性能相关的部分用JavaScript实现。Buffer的本质就是字节...
适用于nodejs的Web API兼容 。 与替代品的比较 该库存在的原因是因为选择使用nodejs本机流来损害的Web API兼容性。 我们发现,在跨nodejs和浏览器rutime共享代码时,这是有问题的。 相反,该库通过使用来自库的实现来符合规范,即使这在nodejs上下文中不太方便。 注意:节点流和Web 实现AsyncIterable接口,并且理论上都可以循环。 然而,实际上,主流浏览器尚未提供对ReadableStream的AsyncIterable支持,根据我们的经验,由节点获取做出的选择是不切实际的。 fetch-blob围绕节点Buffer构建。 此实现基于标准的Uint8Array构建。 fetch-blob选择使用WeakMap封装私有状态。 该库选择使用名称以_开头的属性。 尽管这些属性并不是真正的私有属性,但它们确实具有更
nodejs处理blob格式数据nodejs读取到blob数据转换成utf-8字符串关于string_decoder(字符串解码器)的知识 nodejs读取到blob数据转换成utf-8字符串 首先得到的数据形式要是一个Buffer对象(如果不是Buffer对象,就需要转换Buffer对象 nodejs中文网.了解) 2.引入模块string_decoder,创建StringDecoder对象 const { StringDecoder } = require('string_decoder'); function base64ToArrayBuffer(base64) { const binaryString = window.atob(base64); const len = binaryString.length; const bytes = new Uint8Array(len); for (let i = 0; i < len; i++) { bytes[i] = binaryString.charCodeAt(i); return bytes.buffer; 2. 使用 ArrayBuffer 对象创建 Blob 对象。 function createBlobFromBase64(base64, mimeType) { const arrayBuffer = base64ToArrayBuffer(base64); return new Blob([arrayBuffer], { type: mimeType }); 其中,`base64` 参数是 Base64 编码的字符串,`mimeType` 参数是 Blob 对象的 MIME 类型。返回的对象是一个 Blob 对象。