简体   繁体   中英

How can my Firefox extension download a partially downloaded file?

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:

  • Use nsIChannel / nsIHttpChannel directly.
  • Construct a channel via nsIIOService.newChannel*() .
  • Try to QueryInterface the channel to nsIHttpChannel .
  • Either use 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.
  • Implement a nsIStreamLister / nsIRequestObserver , and call channel.asyncOpen(listener, ...) with it.
  • In 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.
  • In listener.onDataAvailable , write the data to your file. Also check that the server does not send too much data.
  • In listener.onStopRequest handle any failure (connection cut, amount transferred). Close your file. Notify the user or whatever.
  • You probably need to implement a lot of other stuff eg to handle redirects, http-auth, etc.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM