How to create a new file and save it with ArrayBuffer content?

I have an arraybuffer which i want to save into a new file so i can use them later for other things.

how can i store the ArrayBuffer in a txt file and choose the location to save it?

1 Like

You can’t save files with JavaScript directly as it doesn’t have access to the file system from within the browser - you’ll have to use a server side language such as PHP if you want to save the file in the traditional sense (i.e. to a directory on your computer).
Alternatively have a look at Web Storage and see if that would suit your needs.

Although if you want a popup box that asks the user where they want to save the file, that is possible.

const link = document.createElement( 'a' );
link.style.display = 'none';
document.body.appendChild( link );


const blob = new Blob( [ yourArray ], { type: 'text/plain' } );	
const objectURL = URL.createObjectURL( blob );
 
link.href = objectURL;
link.href = URL.createObjectURL( blob );
link.download =  'data.json';
link.click();
 
2 Likes

i tried this but it partially worked for me. is there another wa other than
blob. my content is an ArrayBuffer. so i want to save it like this in the
file too.

blob aparently changes the formatting slightly.

Try changing the Blob’s type from ‘text/plain’ - I think for an ArrayBuffer it should be '‘application/octet-binary’.

var file = new Blob([response], {type: "application/octet-binary;charset=utf-8"});
		var a = document.createElement("a"), url = URL.createObjectURL(file);
        a.href = url;
        a.download = filename;
		document.body.appendChild(a);
		a.click();

this didnt work either. it is still complaining about the format.

Can you post more of your code? It’s hard to know what’s going on without it.

It work for me. The mimeType for binary is application/octet-stream