nsIUploadChannel and nsIFileChannel

1 view
Skip to first unread message

Matthew Buckett

unread,
Jan 3, 2003, 11:02:52 AM1/3/03
to
I'm trying to get some code that allows saving of some data
(http/ftp/file URLs) and it works for nsIHttpChannel and nsIFtpChannel
but nsIFileChannel doesn't seem to like it. I get no file created and an
error of 0x8000ffff.

Here's the code:

var dataString = "This is a test string\n";

var ioService = Components.classes[
"@mozilla.org/network/io-service;1"
].getService(Components.interfaces.nsIIOService);

var nsIURI Components.classes[
"@mozilla.org/network/standard-url;1"
].createInstance(Components.interfaces.nsIURI);

nsIURI.spec = "file:///tmp/test.file";

var ioChannel = ioService.newChannelFromURI(nsIURI);
var destChannel = ioChannel.QueryInterface(
Components.interfaces.nsIFileChannel
);

var uploadChannel = destChannel.QueryInterface(
Components.interfaces.nsIUploadChannel
);

var postStream = Components.classes[
"@mozilla.org/io/string-input-stream;1"
].createInstance(Components.interfaces.nsIStringInputStream);

postStream.setData(dataString, dataString.length);
uploadChannel.setUploadStream(postStream, "text/plain", -1);

uploadChannel.asyncOpen(listener, null);


Thank you for any help.

Matthew Buckett

Boris Zbarsky

unread,
Jan 3, 2003, 12:26:07 PM1/3/03
to Matthew Buckett, mozilla...@mozilla.org
> var nsIURI Components.classes[
> "@mozilla.org/network/standard-url;1"
> ].createInstance(Components.interfaces.nsIURI);
>
> nsIURI.spec = "file:///tmp/test.file";

This is the wrong way to create a URI and in particular it creates a file://
uri that does not know that it's a file uri (does not QI to nsIFileURL).

You want to use nsIIoService::newURI to create the URI object.

Boris
--
Ray's Rule of Precision:
Measure with a micrometer. Mark with chalk. Cut
with an axe.


Reply all
Reply to author
Forward
0 new messages