Downloading file from DataURL in JavaScript
Asked Answered
P

2

7

From this string we get from DataURL, what's the best way to download this as a file?

So far what I got was using a basic window.open("myDataURL");, but I'm not able to change the file name in this way.

window.open('data:application/msword;base64,0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAA
             PgADAP7/CQAGAAAAAAAAAAAAAAACAAAANQAAAAAAA
             AAAEAAANwAAAAIAAAD+////AAAAADQAAABsAA/',
             '_blank','height=300,width=400');

I was wondering if there's any way to handle this data properly.

Placebo answered 9/1, 2012 at 17:33 Comment(3)
See https://mcmap.net/q/742452/-cross-browser-save-as-txtInfusible
I've seen this question before, the file name becomes "rVPjLUq1.part", just as if I were using the example I posted above.Placebo
As Wladimir Palant writes, the issue has been discussed on the W3C mailing list, but "this doesn't seem to have made it into any specification so far, let alone browser implementations." I'm afraid there hasn't been any progress in that matter.Infusible
T
3

you can add a download attribute to the anchor element. sample:

<a download="abcd.zip" href="data:application/stream;base64,MIIDhTCCAvKg........">download</a>
Tojo answered 13/9, 2012 at 14:52 Comment(3)
This doesn't support in IEHightension
It also doesn't address the OP's question of specifying a filename to the window.open attribute (from Javascript, not HTML).Chromato
clicking the link will open the download dialog, need not window.openTojo
D
0

Try this:

data:application/msword;fileName=test.doc;base64,0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAACAAAANQAAAAAAAAAAEAAANwAAAAIAAAD+////AAAAADQAAABsAA/

But this is just a guess from googling around and might be browser-dependent. The real answer to this is, you can't - See http://www.ietf.org/rfc/rfc2397 for reference, there's nothing in the specification to support a filename.

Delightful answered 9/1, 2012 at 18:9 Comment(4)
Yea, it didn't work. Is it just meant for images usage? Because it's not a big advantage if you can't reuse this "value" and recreate a file with it. I'm still looking for answers to fix this problems...using JSP or Java as well. But I can't see no links on how to handle dataURL in this way.Placebo
Data URIs are meant for handling raw data in an URI compatible way. Like for example embedding of images or text into another document that would otherwise expect an external linked element.Delightful
I see. In our method to get the file content, we use our reader.readAsDataURL(file);...If I use it readAsBinaryString instead, will I be able to to recreate my files(pdf, xls, docs) properly after?Placebo
the fileName parameter is ignored. The files are saved as [random string].partJaqitsch

© 2022 - 2024 — McMap. All rights reserved.