Re: [request] "Download" Event for HTMLAnchorElement

On Mon, Mar 24, 2014 at 2:45 PM, Si Robertson <retromodular@gmail.com>
 wrote:

> If a developer creates an object URL, e.g. theFile =
> URL.createObjectURL(theData), then it's the developer's responsibility to
> revoke that object URL. Assigning theFile to an anchor href so the data
> can be downloaded doesn't create a copy of data.
>

Requiring the user to revoke blob URLs is manual resource management, which
is inherently brittle in JS.  The eventual solution should be auto-revoke
URLs, which makes it the browser's job to revoke the URL.  We should make
that work fully, not add more events to allow manual revocation.

Autorevoke URLs are in, but the mechanism for the browser to see "a.href =
myAutoRevokeObjectURL" and keep the URL alive correctly is still being
worked on (https://www.w3.org/Bugs/Public/show_bug.cgi?id=17765).

That said:

On Mon, Mar 24, 2014 at 2:45 PM, Si Robertson <retromodular@gmail.com>wrote:

> The web browser will definitely know when the data has been written to
> disk, the problem is the developer won't know when the data has been
> written to disk, so it's currently impossible to revoke the object URL
> (release the data from memory) at a safe time. When the anchor is clicked
> you could create a timer and revoke the object URL after a certain number
> of seconds have elapsed but you will still be taking a shot in dark.
>

If you don't care about the user clicking the link more than once, it
should always be safe to release the object URL immediately after the click
event has completed.  If the browser has begun downloading (copying to
disk) the blob, that operation should be unaffected by you releasing the
object URL.  You would just need to use a zero-second timer, so that you
release it after the click event has completed and not while it's being
dispatched.

(If you do something like this, be sure to hide the download link and at
least replace it with a "Download in progress", or better a "Click here to
restart download" to regenerate the blob, so you don't leave a broken
anchor on the page.)

This may not  work in browsers yet, and this may not be well-defined in the
spec.  If it's not, I'm guessing it'll be fixed as part of
https://www.w3.org/Bugs/Public/show_bug.cgi?id=24338.

-- 
Glenn Maynard

Received on Monday, 24 March 2014 20:17:07 UTC