Mozilla has some code snippets for downloading files from a Firefox extension .
I didn't see how to finish downloading partially downloaded files similar to wget -c
.
Is there some way to continue downloading a partially downloaded file from a Firefox extension?
The documentation for nsIWebBrowserPersist.saveURI()
has a parameter aExtraHeaders
. Do I just use the HTTP header Range
there?
The nsIWebBrowserPersist
API does not support resuming.
You could try to use the built-in download manager . Or at least the code for it might be helpful.
Otherwise, you have to go more low-level than that. In the grand scheme of things, you'll need:
nsIChannel
/ nsIHttpChannel
directly. nsIIOService.newChannel*()
. QueryInterface
the channel to nsIHttpChannel
. nsIHttpChannel.setRequestHeader
, or QI
again and use nsIResumableChannel.resumeAt()
. The latter requires you to know the the entityID (ETag for http) beforehand, but will take care of a bunch of stuff when used, like verifying the server does actually respond with a ranged response. nsIStreamLister
/ nsIRequestObserver
, and call channel.asyncOpen(listener, ...)
with it. listener.onStartRequest
, you'll want to double check that the request is good to go (The server actually replied 206
, and so on). Also, time to open the file, either via an nsIFileStream or something like OS.File (the latter is preferred these days for snappiness-reasons). See MDN for more info. Don't forget to seek to the appropriate location within the file. listener.onDataAvailable
, write the data to your file. Also check that the server does not send too much data. listener.onStopRequest
handle any failure (connection cut, amount transferred). Close your file. Notify the user or whatever. As you can see, the amount of work you need to do and amount of code you need to write is non-trivial.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.