In my XulRunner based application, I need to send a file to the user
when he clicks on the "Export" button, after showing him a "Save file"
dialog where he can choose the local destination's path. With a small
file, it works. But when the file becomes bigger, only 8 bytes are
written... And I don't understand why. Here is the sample of code, any
idea are welcome:
var nsIFilePicker = Components.interfaces.nsIFilePicker;
var filepicker = Components.classes["@mozilla.org/filepicker;
1"].createInstance(nsIFilePicker);
filepicker.init(window, "Save file to...", nsIFilePicker.modeSave);
var res = filepicker.show();
if (res == nsIFilePicker.returnOK) {
var ioService = Components.classes["@mozilla.org/network/io-service;
1"].getService(Components.interfaces.nsIIOService);
var channel = ioService.newChannel(url,null,null);
var input = channel.open();
var inStream = Components.classes["@mozilla.org/scriptableinputstream;
1"].getService(Components.interfaces.nsIScriptableInputStream);
inStream.init(input);
var data = inStream.read(input.available());
var outStream = Components.classes["@mozilla.org/network/file-output-
stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
outStream.init(filepicker.file, 0x02 | 0x08 | 0x20, 0664, 0); //
write, create, truncate
outStream.write(data, data.length);
outStream.close();
inStream.close();
input.close();
}
Thanks a lot for reading, and thanks for any anwser!
1) There's no guarantee that this will get all the data. Typically, it will
only get the first packet.
2) If the data contains null bytes, what you get out will be truncated at the
first null.
-Boris
You should probably look into nsIDownloader or nsIWebBrowserPersist.
Thanks for your answer, I now understand what was wrong.
On 25 avr, 17:39, Christian Biesinger <cbiesin...@web.de> wrote:
> You should probably look into nsIDownloader or nsIWebBrowserPersist.
I already tried but it didn't work.
I finally discovered jslib (http://jslib.mozdev.org/) which seems more
developer friendly for this kind of operations. So I'm using a jslib
RemoteFile and a jslib File: it works but when the content is not
ASCII (I mean, PDF, XLS...), it is corrupted and/or not complete. Here
is my code:
===
include (jslib_remotefile);
var srcUrl = "http://localhost/sample.pdf";
var srcFile = new RemoteFile(srcUrl);
if (srcFile.open())
{
var content = srcFile.read();
include ('chrome://jslib/content/io/file.js');
var destPath = "/tmp/sample.pdf";
var destFile = new File(destPath);
if (destFile.exists() == false)
{
destFile.create();
}
destFile.open('w');
destFile.write(content);
destFile.close();
srcFile.close();
}
===
I also tried with the FileUtils.copy() method, but it only works with
local files. What I would like is just something similar to the PHP's
file_get_contents(). Any idea?
Thanks for reading, have a good week-end!