Search code examples
javascriptxpageslotus-notes

XPages create a new Folder on button click


I have a file upload control that saves a file directly to the server instead of a Notes Document, However, the folder that the file saves in has to be created beforehand. Is there a way to create a new folder and save the file to that folder instead of a folder that already exists.

See Code below:

XPages

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">

    <xp:this.resources>
        <xp:script src="/scriptsTest.jss" clientSide="false"></xp:script>
    </xp:this.resources>
    <xp:fileUpload id="fileUpload1"></xp:fileUpload>
    <xp:button value="Label" id="button1">
        <xp:eventHandler event="onclick" submit="true"
            refreshMode="complete">
            <xp:this.action><![CDATA[#{javascript:saveTest();}]]></xp:this.action>
        </xp:eventHandler>
    </xp:button>
    <xp:text escape="true" id="message"></xp:text></xp:view>

Javascript

function saveTest()
{
    var con = facesContext.getExternalContext(); 
    var request:com.sun.faces.context.MyHttpServletRequestWrapper = con.getRequest(); 
    var map:java.util.Map = request.getParameterMap(); 
    
    var fileDataName = getClientId("fileUpload1");
    var fileData:com.ibm.xsp.http.UploadedFile = map.get( fileDataName ); 
    
    if (fileData == null) {
        getComponent("message").value = "File could not be found on " + fileDataName;
        return;
    }

    var fileName = fileData.getServerFileName();
    var serverFile:java.io.File = fileData.getServerFile();
    if (serverFile != null && serverFile.exists()) {
         var dir = "/home/notesdata/Shared/XBRL"; //Want to create a folder programmatically called Test in XBRL folder so path should be /home/notesdata/Shared?XBRL/Test

        var tempFile:java.io.File = new java.io.File(fileName);
        var correctedFileName = dir + java.io.File.separator + fileData.getClientFileName();
         var correctedFile:java.io.File = new java.io.File( correctedFileName ); 
        var success = tempFile.renameTo(correctedFile);
        getComponent("message").value = "Yay!" + correctedFileName;
        //correctedFile.renameTo(tempFile);
    }
    else {
        getComponent("message").value = "There's a problem to find the temporal file.";
    }
}

Solution

  • Use mkdir() to create a new folder in a path that already exist. So the following will create a new folder called Test in /home/notesdata/Shared/XBRL if that path already exists:

    var newPath = "/home/notesdata/Shared/XBRL/Test";
    var newFolder:java.io.File = new java.io.File(newPath);
    newFolder.mkdir();
    

    Notice: use mkdirs() to create a new folder hierarchy that doesn't already exist.