Tags: xxx

How to Convert String to Blob

Jul 23, 2021 1 min.

The simplest way is to use the Blob constructor. To convert a string to Blob const str = 'hello world'; const blob = new Blob([str], { type : 'plain/text' }); To convert a JSON to Blob const obj = { hello: 'world' }; const blob = new Blob([JSON.stringify(obj, null, 2)], { type : 'application/json' }); Another way is use fetch if your content is in base64 encode data const base64str = "aGVsbG8gd29ybGQ="; const blob = await fetch(`data:plain/text;base64,${base64str}`).