messi
messi

Reputation: 11

How can i create a file and save it in local system using Blob in javascript?

During implementation of file transfer use case through WebRTC protocol, i am recieving data from queue at reciever end in some variables but unable to use that. Through some digging, i came to know that it can be done using Blob, code snippet that i used :

var data=reciever.dequeue();
if(data)
{ var blob = new Blob(_base64ToArrayBuffer(data), {type: 'text/plain'});
  // need to know how to proceed now?
}

file is need to be saved in local system.Thanks in advance.

Upvotes: 0

Views: 746

Answers (1)

Tyler.z.yang
Tyler.z.yang

Reputation: 2450

You can create a temp anchor element element and append it to document.body, trigger click event. Done.

Here is the demo code:

var url = objectURL = URL.createObjectURL(blob); //your blob object here
var anchor = document.createElement("a");
anchor.href = url;
anchor.download = "YourFileName";
anchor.click(); //This will trigger browser download event.

Here is document about blob to URL.

Here is the blob document.

Hope this works. : )

Upvotes: 1

Related Questions