Saturday, February 19, 2011

Is there any way to specify a suggested filename when using data: URI?

If for example you follow the link:

data:application/octet-stream;base64,SGVsbG8=

The browser will prompt you to download a file consisting of the data held as base64 in the link itself. Is there any way of suggesting a default file-name for the browser to use? If not, is there a javascript solution?

I'm referring to HTML hyperlinks.

From stackoverflow
  • According to RFC 2397, no, there isn't.

    Nor does there appear to be any attribute of the <a> element that you can use either.

  • I was unsure of what you meant by 'the link' as I don't see one. But if you add this Header to the response:

    Content-Disposition: attachment; filename=somefilenameijustcameupwith.doc;

    In C# this is done with: Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

    Alnitak : that works if you're downloading a separate file from the server. The point of the "data:" URI is that the data is embedded in the link itself, so no further HTTP request occurs. Try pasting his URI in your browser to see what I mean.
    Alnitak : I should say that _only_ works if you're downloading a separate file. It _won't_ work with a data URI, so is not a useful answer.
    postback : So indeed, I misunderstood. Like I mentioned, I was unsure. So the other answer is the correct one.

0 comments:

Post a Comment