I want to use the download manager in trunk 1.9, but I do something
wrong. I get an exception: NS_NOINTERFACE
Probably one of the arguments of my call to addDownload of the download
manager does not match what is expected, but I do not understand which
is wrong.
I want to use the download manager instead of the nsIDownloader, because
I need a download progress, which seems not to be provided by the
nsIDownloader.
My attempt is this:
// progress listener
var pl =
{
onDownloadStateChange:function(state,download){
dump('onDownloadStateChange'+state);}
, onProgressChange
:function(webProgress,request,curSelfProgres,maxSelfProgress
,curTotalProgres,maxTotalProgress,download){
dump('onProgressChange'+curSelfProgres+' of '+maxSelfProgres)}
, onSecurityChange:
function(webProgress,request,state,download){
dump('onSecurityChange'+state);}
, onStateChange:
function(webProgress,request,stateFlags,status,download){
dump('onStateChange'+state);}
}
// nsIURI
var src
=Components.classes["@mozilla.org/network/simple-uri;1"]
.createInstance(Components.interfaces.nsIURI);
src.spec='http://localhost/mei.zip';
// nsIURI
var dest
=Components.classes["@mozilla.org/network/simple-uri;1"]
.createInstance(Components.interfaces.nsIURI);
dest.spec='file:///Volumes/stick/mei.zip';
// nsICancelable
var cnc = {cancel:function(c){}};
// nsIDownloadManager
var dm =
Components.classes["@mozilla.org/download-manager;1"]
.createInstance(Components.interfaces.nsIDownloadManager);
dm.addListener(pl);
var dl = dm.addDownload(0,src,dest,'test Download', null, new
Date().getTime(),null, cnc);
This is, what I get:
Exception... "Component returned failure code: 0x80004002
(NS_NOINTERFACE) [nsIDownloadManager.addDownload]"
If I use the downloader instead of download manager, download succeeds,
but I don't get any download progress, which might cause impatient users
to abort.
Which has the addDownload method you mentioned, it returns an
nsIDownload: http://developer.mozilla.org/en/docs/nsIDownload
From there you can use the nsIDownloadManager to call addListener using
your own implementation of nsIDownloadProgressListener:
http://developer.mozilla.org/en/docs/nsIDownloadProgressListener
onProgressChange, and onDownloadStateChange are the two you probably
care the most about.
(See Also: http://developer.mozilla.org/en/docs/nsIWebProgressListener
which is used here as well)
--
~Justin Wood (Callek)
Cheers,
Shawn Wilsher
The explanation is bogus; the original URI wasn't a file URI because the
IO service hadn't been used to create it.
--
Warning: May contain traces of nuts.
> // nsIURI
> var src
> =Components.classes["@mozilla.org/network/simple-uri;1"]
> .createInstance(Components.interfaces.nsIURI);
> src.spec='http://localhost/mei.zip';
>
> // nsIURI
> var dest
> =Components.classes["@mozilla.org/network/simple-uri;1"]
> .createInstance(Components.interfaces.nsIURI);
> dest.spec='file:///Volumes/stick/mei.zip';
Only protocol handlers are allowed to do this. Everyone else MUST use
the IO service to create new URIs.
See also
http://developer.mozilla.org/en/docs/Common_Pitfalls#How_to_create_a_URI_object
-Boris