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.

Ry-
  • 218,210
  • 55
  • 464
  • 476
eBergamo
  • 101
  • 3
  • 6
  • See http://stackoverflow.com/a/6465780/27862 – user123444555621 Jan 09 '12 at 17:46
  • I've seen this question before, the file name becomes "rVPjLUq1.part", just as if I were using the example I posted above. – eBergamo Jan 09 '12 at 18:02
  • As Wladimir Palant writes, the issue has been [discussed on the W3C mailing list](http://lists.w3.org/Archives/Public/uri/2010Feb/thread.html#msg58), 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. – user123444555621 Jan 10 '12 at 10:51

2 Answers2

3

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

<a download="abcd.zip" href="data:application/stream;base64,MIIDhTCCAvKg........">download</a>
cuixiping
  • 24,167
  • 8
  • 82
  • 93
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.

bardiir
  • 14,556
  • 9
  • 41
  • 66
  • 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. – eBergamo Jan 09 '12 at 18:19
  • 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. – bardiir Jan 09 '12 at 18:22
  • 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? – eBergamo Jan 09 '12 at 18:41
  • 1
    the `fileName` parameter is ignored. The files are saved as `[random string].part` – powtac Mar 01 '12 at 21:12