Home > Web Front-end > Front-end Q&A > nodejs blob to file

nodejs blob to file

WBOY
Release: 2023-05-11 17:03:37
Original
2521 people have browsed it

In front-end development, we may encounter the need to convert Blob objects into files. Blob is an object type in Web API that can represent arbitrary binary data. A file is an entity with attributes such as file name, file type, file size, etc. We often need to convert the Blob object into a file type through the browser for uploading, saving and other operations.

In the Node.js environment, we can use Node's built-in module fs to perform file operations, and we can use Buffer to process binary data. Therefore, we can convert the Blob object into a file by converting the Blob object to a Buffer, and then use the fs module to write the Buffer to the file.

The specific implementation steps are as follows:

  1. Get the Blob object. We can get the Blob object through the following code:
let blob = new Blob([arrayBuffer], { type: 'image/png' });
Copy after login

Here a Blob object containing binary data is created.

  1. Convert Blob to Buffer. We can convert Blob objects into Buffers with the help of the buffer module in Node.js. The specific code is as follows:
const buffer = Buffer.from(await blob.arrayBuffer());
Copy after login

The arrayBuffer() method of Blob is used to obtain binary data and then convert it into a Buffer.

  1. Write to file. Using the fs module in Node.js, we can write the Buffer to a file. The specific code is as follows:
fs.writeFile('test.png', buffer, (err) => {
  if (err) throw err;
  console.log('The file has been saved!');
});
Copy after login

Here the Buffer is written to a file named "test.png". If written If there is an error in the file, an exception will be thrown, otherwise "The file has been saved!" will be output on the console.

The complete code is as follows:

const fs = require('fs');
const fetch = require('node-fetch');

async function downloadFile(url) {
  const res = await fetch(url);
  const blob = await res.blob();
  const buffer = Buffer.from(await blob.arrayBuffer());
  fs.writeFile('test.png', buffer, (err) => {
    if (err) throw err;
    console.log('The file has been saved!');
  });
}

downloadFile('https://www.example.com/test.png');
Copy after login

This code can download the image file on the remote server to the local and save it as the file "test.png".

Summary:

This article introduces how to convert Blob objects into files. In the Node.js environment, we can use the Buffer and fs modules to achieve this function. This method is very useful when we need to convert the Blob object in the front end to a file, or when we need to process binary data and save it as a file in the Node.js environment.

The above is the detailed content of nodejs blob to file. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template