[Jslib] File Picker in jslib

Ryan Maw rmaw at forge-industrial.com
Tue Oct 7 13:44:39 EDT 2003


Pete Collins wrote:

>> Gary Frederick wrote:
>>
>>> Is there a way to get the filepicker with jslib?
>>>
>>> Would it be useful to add it to jslib if there is not a way to 
>>> 'pick' a file already there?
>>
>>
>>
>> I started some work on one a while back.
>>
>>
>> http://www.mozdev.org/lxr/http/source///jslib/libraries/xul/commonFilePicker.js 
>>
>>
>> Idl interface is here:
>>
>>   
>> http://lxr.mozilla.org/seamonkey/source/widget/public/nsIFilePicker.idl
>>
>> I only implemented a saveAsXUL method.
>>
>>
>> http://www.mozdev.org/lxr/http/source/jslib/libraries/xul/commonFilePicker.js#L58 
>>
>>
>> It would be nice to finish it.  :)
>> .
>>
>> --pete
>>
I have built a set of SaveAs and Save functions/methods a custom 
document object in an app I have built.  Here they are if you want them, 
maybe they will help...

mawrya


//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//  XDOC SAVE AS  -- (sSavePath) must be of the form:  
chrome://something/somthing/foo.xxx   OR file:///some/path/name/foo.xxx 
OR http:// (http not supported yet)
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
XDoc.prototype.saveAs = function (sSavePath){

       //IF THE PERSON PASSES IN A PATH FOR sSavePath THEN SKIP TO THE 
SAVE FUNCTION
        if(sSavePath != null){
                this.setURL(sSavePath);
                this.save();
       
        }else{
                const nsIFilePicker = Components.interfaces.nsIFilePicker;
                var fp = 
Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker); 


                fp.init(window, title, nsIFilePicker.modeSave);
                fp.appendFilters(nsIFilePicker.filterText | 
nsIFilePicker.filterAll);
            var sUserCommand = fp.show();
                if ((sUserCommand == nsIFilePicker.returnOK) || 
(sUserCommand == nsIFilePicker.returnReplace))
                {
                        //SET NEW FILE URL AND SAVE IT
                        ///alert("SaveAs returned: " + 
fp.fileURL.prePath + fp.fileURL.path);
                        ///alert("SaveAs returned: " + 
fp.fileURL.prePath + fp.fileURL.file.path);
                        sSaveURL = new String(fp.fileURL.prePath + 
fp.fileURL.path);
                        var re = new RegExp("%20","g");
                        sSaveURL = sSaveURL.replace(re," ");
                        this.setURL(sSaveURL.toString());
                        this.save();  //SEE SAVE METHOD BELOW
                }
        }
};



//-------------------------------------------------------------------------------------------------------------------------------------------------------------------
//    XDOC SAVE
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------
XDoc.prototype.save function() {
              
                //  DETERMINE WHETHER TO DISPLAY A "Save As..." DIALOG BOX
                sCurrentFileName = new String(this.fileName);
                if (((sCurrentFileName.substring(0,9) == "Untitled[") || 
(this.fileName == "") || (this.fileName == undefined) || (this.URL == 
undefined) || (this.URL == null) || (this.URL == ")))       
               {
                          this.saveAs();
                          return;
                }

                //DETERMINE WHAT TYPE OF URL WE ARE DEALING WITH AND 
FORMAT IT A FILE PATH FOR SAVING
                sSaveURL = new String(this.URL);
               var sURLPrePath = 
sSaveURL.substring(0,sSaveURL.indexOf("://")+3);
                switch (sURLPrePath) {
                    case "file://":
                                    ///alert("found file:// url: " + 
sSaveURL.toString());
                                    ///alert("fileURL to path: " + 
sSaveURL.substring(8));
                                    sSaveURL = new 
String(sSaveURL.substring(8));
                                    break;
                    case "chrome://":
                                    var fu = new FileUtils();
                                    ///alert("chrome to path: " + 
fu.chromeToPath(sSaveURL.toString()));
                                    sSaveURL = new 
String(fu.chromeToPath(sSaveURL.toString()));
                                    break;
                    case "http://":
                                    this.saveAs();
                                    return;
                    case "https://":
                                    this.saveAs();
                                    return;
                }
               //CODE TO CHANGE TO MS WINDOWS FILE PATH NOTATION (SKIP 
THIS FOR *NIX)
                var re = new RegExp("/","g");
                sSaveURL = sSaveURL.replace(re,"\\");
                sSaveURL = sSaveURL.toString();
               
                // 3) SAVE AS A FILE
                try{var f=new File(sSaveURL)}catch(e){alert("Error:  
XDoc -- XDocSave() -- Create file failed:\n" + sSaveURL + "\n" + e)}
                try{f.exists() }catch(e){alert("Error -- XDocSave() -- 
Bad file path, unable to save file:\n" + sSaveURL + "\n" + e)}
                try{ if(f.exists()){f.remove();} }catch(e){alert("Error 
-- XDocSave() -- Unable to overwrite existing file.  Check file 
permissions." + "\n" + e);}
                try{f.create();}catch(e){alert("Error -- XDocSave() -- 
Unable to create new file.  Check directory write permissions." + "\n" + e)}
                try{f.open('w')}catch(e){alert("Error:  XDoc -- 
XDocSave() -- Open file failed" + "\n" + e)};
                try{f.write(---FEED IN THE FILE CONTENT FROM A STRING 
HERE----)}catch(e){alert("Error:  XDoc -- XDocSave() -- Unable to write 
file" + "\n" + e);}
                try{f.close()}catch(e){alert("Error:  XDoc -- XDocSave() 
-- Unable close filesystem file object" + "\n" + e);}
                this.saved = true;
                return true;    
}





More information about the Jslib mailing list