I am writing a firefox extension which involves downloading and merging
Binary (Jpeg) files.
So far I Can download the files, but for some reason am unable to merge
them.
The output (merged file) is always 0 bytes.


 (Note I am making use of JSlib to open the file in "binary append"
mode and to write to the file. but even if i use simple xpcom functions
and merely try to read from one file and write to the other, without
merging, i still get the output file to be of 0 Bytes)

The relevent piece of code follows:


var nsIDownloadObserver =
    {onDownloadComplete: function(nsIDownloader, nsresult, nsIFile)
        {//dump( "\nDownloadComplete" );
            alert("Download Complete. Scan " + scanNum + "
downloaded");

            var fileOut =
Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
            fileOut.initWithPath("C:\\Documents and
Settings\\sina\\Desktop\\image.jpg");
            //if the ourput file does not exist (ie this is the first
scan) then create it
            if(fileOut.exists()==false)
{fileOut.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0777);}


            //the following is code to read and write a binary file.
plz shift this to the xpcomBinaryFileRead/Write functions.

            var fileInPath = "file://C:\\Documents and
Settings\\sina\\Desktop\\temp_image.jpg"
            var fileOutPath = "file://C:\\Documents and
Settings\\sina\\Desktop\\image.jpg"

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

            var fileInChannel = ios.newChannel(fileInPath, "", null);
            var binaryIS =
Components.classes["@mozilla.org/binaryinputstream;1"].createInstance(Components.interfaces.nsIBinaryInputStream);
            binaryIS.setInputStream(fileInChannel.open());

            //Open the file in append-binary mode(function from JSLib)
            fileOut.open("ab");

fileOut.write(binaryIS.readByteArray(binaryIS.available()));
            BinaryIS.close();
            //BinaryOS.close();
            alert("Scan " + scanNum + " Appended");
        }
    };
//var nsIDownloadObserver = {onDownloadComplete:
function(nsIDownloader, nsresult, nsIFile) {alert("done!");}};





============================================================================
The Entire Code follows:




function savePrompt(url, savefile, filename, autoLocation)
{

    try {

netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    } catch (e) {
        alert("Permission to save file was denied.");
    }

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

    var fileHandler =
ios.getProtocolHandler("file").QueryInterface(Components.interfaces.nsIFileProtocolHandler);
    var nsIFile = fileHandler.getFileFromURLSpec("file://C:\\Documents
and Settings\\sina\\Desktop\\temp_image.jpg");

    var outputStream = Components.classes[
"@mozilla.org/network/file-output-stream;1" ].createInstance(
Components.interfaces.nsIFileOutputStream );


    var nsIDownloadObserver =
    {onDownloadComplete: function(nsIDownloader, nsresult, nsIFile)
        {//dump( "\nDownloadComplete" );
            alert("Download Complete. Scan " + scanNum + "
downloaded");

            var fileOut =
Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
            fileOut.initWithPath("C:\\Documents and
Settings\\sina\\Desktop\\image.jpg");
            //if the ourput file does not exist (ie this is the first
scan) then create it
            if(fileOut.exists()==false)
{fileOut.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0777);}


            //the following is code to read and write a binary file.
plz shift this to the xpcomBinaryFileRead/Write functions.

            var fileInPath = "file://C:\\Documents and
Settings\\sina\\Desktop\\temp_image.jpg"
            var fileOutPath = "file://C:\\Documents and
Settings\\sina\\Desktop\\image.jpg"

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

            var fileInChannel = ios.newChannel(fileInPath, "", null);
            var binaryIS =
Components.classes["@mozilla.org/binaryinputstream;1"].createInstance(Components.interfaces.nsIBinaryInputStream);
            binaryIS.setInputStream(fileInChannel.open());

            //var fileOutChannel = ios.newChannel(fileOutPath, "",
null);
            //var binaryOS =
Components.classes["@mozilla.org/binaryoutputstream;1"].createInstance(Components.interfaces.nsIBinaryOutputStream);
            //binaryOS.setOutputStream(fileOutChannel.open());

            //var Buffer =
binaryIS.readByteArray(binaryIS.available());
            //binaryOS.writeByteArray(buffer,buffer.length);
            //binaryOS.flush();

            //Open the file in append-binary mode(function from JSLib)
            fileOut.open("ab");

fileOut.write(binaryIS.readByteArray(binaryIS.available()));
            BinaryIS.close();
            //BinaryOS.close();
            alert("Scan " + scanNum + " Appended");
        }
    };

    //Figure out which scan to fetch. hardcoded for now
    var thUrl = "";

    if(scanNum==2) scanNum = 0;
    if(scanNum == 1)
    {
        theUrl = "http://individual.utoronto.ca/arpit/2.jpg";;
        //theUrl = "http://individual.utoronto.ca/arpit/2.txt";;
    }

    if(scanNum==0)
    {
        theUrl = "http://individual.utoronto.ca/arpit/1.jpg";;
        //theUrl = "http://individual.utoronto.ca/arpit/1.txt";;
    }
    scanNum++;


    var nsIDownloader =
Components.classes["@mozilla.org/network/downloader;1"].createInstance();
    nsIDownloader.QueryInterface(Components.interfaces.nsIDownloader);
    nsIDownloader.init( nsIDownloadObserver, nsIFile );

    //nsIHttpChannel =
ios.newChannel("http://pschuler.club.fr/Cinema/Actrices/Aishwarya-Rai/aishwarya-rai3.jpg";,
"", null);

    nsIHttpChannel = ios.newChannel(theUrl, "", null);

nsIHttpChannel.QueryInterface(Components.interfaces.nsIHttpChannel);
    nsIHttpChannel.requestMethod = "GET"; // Optional as is default

    nsIHttpChannel.asyncOpen( nsIDownloader, nsIFile ); 
}

_______________________________________________
Mozilla-xpcom mailing list
Mozilla-xpcom@mozilla.org
http://mail.mozilla.org/listinfo/mozilla-xpcom

Reply via email to