3

Right now I am creating a voice search feature. So the plan is I will record the audio using ReactJS, convert the Blob file into a .wav file. Then once the file is created I will save it locally on my desktop. Once the file is on the desktop, I will use the OS module in python to access the file on my desktop to interpret the audio file into text.

So everything is essentially done, the only problem that I am having is I can't figure out how to use ReactJS to save the file on my desktop.

Can someone please help me with this?

5
  • 2
    stackoverflow.com/questions/5192917/… second answer down <a download="name_of_downloaded_file" href="path/to/the/download/file"> Clicking on this link will force download the file</a> Commented Feb 6, 2021 at 15:14
  • Did you see that: medium.com/javascript-in-plain-english/… ? Commented Feb 6, 2021 at 15:25
  • Saving a file from the browser to anywhere but the downloads folder is a huge security hole, and it's not allowed. I don't think you'll be able to save to the Desktop. Here's a technique that will save to Downloads. Commented Feb 6, 2021 at 15:39
  • @terrymorse eventually I will use POST to put it on a server but for now I just want it locally. Commented Feb 6, 2021 at 15:52
  • Why not just set up a localhost server? You can save any file to disk with a server, and you'll have written some server code for future use. Commented Feb 6, 2021 at 18:23

3 Answers 3

6

Refer the more detail in web.dev and about MDN blob

Following code will work. (Try loading in chrome dev console or similar)

const saveFile = async (blob) => { const a = document.createElement('a'); a.download = 'my-file.txt'; a.href = URL.createObjectURL(blob); a.addEventListener('click', (e) => { setTimeout(() => URL.revokeObjectURL(a.href), 30 * 1000); }); a.click(); }; const obj = {hello: 'world'}; const blob = new Blob([JSON.stringify(obj, null, 2)], {type : 'application/json'}); saveFile(blob); 

// With react

<div onClick={() => saveFile(blob)}> save file </div> 
Sign up to request clarification or add additional context in comments.

Comments

3

You can use this https://www.npmjs.com/package/file-saver then save the file like this: saveAs(blob, 'filename').

3 Comments

This worked but I can't seem to find a way to save it onto my desktop or a specific folder on the desktop. Can you please help me with that?
@TimScott You can't save a file to anywhere but inside the Downloads folder.
0

once the file is created I will save it locally on my desktop

It is not possible to save a file from the browser to the Desktop, as this would be a huge security vulnerability.

However, you can save a file into the Downloads folder, if the user authorizes it.

This example saves a "Hello World!" text file to the Downloads folder.

Other data types can be saved, using the "data:" URL.

// save a text file named 'hello-world.txt' to Downloads folder document.querySelector('button').onclick = function(evt) { const data = `data:,Hello%2C%20World!`; const filename = 'hello-world.txt'; const aTag = document.createElement('a'); aTag.href = data; aTag.download = filename; aTag.click(); };
<button>Save Hello World!</button>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.