Save a Blob object

144 views
Skip to first unread message

Jumar

unread,
Feb 7, 2022, 1:54:42 AM2/7/22
to DroidScript
How can I save a Blob object into a file. Say I have a doc Blob object, how can I save it as myFile.docx

Regards

Symbroson

unread,
Feb 7, 2022, 3:21:34 AM2/7/22
to DroidScript
In browser js you would download the blob like this:

function downloadBlob(blob, name) {
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.style.display = 'none';
    a.href = url;
    a.download = name;
    document.body.appendChild(a);
    a.click();
    window.URL.revokeObjectURL(url);
}

in NodeJS you could also use blob.text() to get a promise to its contents so smth like could work:

async writeBlob(b, file) {
const txt = await b.text();
await fs.writeFile(file, txt)
}

Jumar

unread,
Feb 12, 2022, 2:38:17 AM2/12/22
to DroidScript
Hi Symbroson

Using  downloadBlob() function on a native DS app hangs the app. Can I use WriteFile with the right config?

Regards

Symbroson

unread,
Feb 12, 2022, 7:53:34 AM2/12/22
to DroidScript
Where do you get the blob object from?

It might be worth trying the blob.text() promise (either with await or via .then())
the blob should have the text data already contained locally

Jumar

unread,
Feb 12, 2022, 8:01:29 AM2/12/22
to DroidScript
The blob object is an excel file. By the way, it works on the browser on a laptop by using the FileSaver.js. But not on DS app nor on DS WebView. FileSaver.js also is not working on DS app WebView.

Regards

Jumar

unread,
Feb 12, 2022, 8:03:20 AM2/12/22
to DroidScript
I got the blob.text through await. And try to save it by app.WriteFile( "myFile.xlsx", blobText ) and I get an excel file with 0 Byte file size. When I try to open it, it says that the file is corrupted.

Regards

Alan Hendry

unread,
Feb 12, 2022, 3:38:02 PM2/12/22
to DroidScript
HI,
I believe MySQL supports BLOBs  (if so then you could try DS database).
Regards, ah

Symbroson

unread,
Feb 12, 2022, 4:10:34 PM2/12/22
to DroidScript
Probably there was some error while reitieving the data. Thats probably why filesaver failed too
where do you get the blob from? Did you download it or do you get it from some api?
Can you give an example code?

Cemal

unread,
Feb 13, 2022, 10:10:00 AM2/13/22
to DroidScript
Hi Jumar,

I am able to save ArrayBuffer with this method:

const file = app.CreateFile(PATH, "rw");
file.WriteData(new Uint8Array(buffer));
file.Close();

If you can't find any other method you can convert Blob to ArrayBuffer.

const buffer = await blob.arrayBuffer();

Reply all
Reply to author
Forward
0 new messages