<?xml version="1.0" encoding="utf-8"?>
<KScriptSetting>
  <Name>file</Name>
  <Description>Provide read and write access to text or binary files under the site folder. Below is fully functioning example.</Description>
  <Example> 
    <![CDATA[
<script engine="kscript">
    if (k.request.method = "POST") {
        if (k.request.files.length > 0) {
            var file = k.request.files[0];
            var filename = file.fileName;
            if (k.request.folder) {
                filename = k.request.folder + "\\" + filename;
            }
            file.save(filename); 
        }
        if (k.request.createFolder) {
            k.file.createFolder(k.request.createFolder, k.request.folder);
        }
    }
    if (k.request.deleteFolder) {
        k.file.deleteFolder(k.request.deleteFolder);
    }
    if (k.request.deleteFile) {
        k.file.delete(k.request.deleteFile);
    }

    var allfiles = k.file.getAllFiles();

    var folderFiles;
    if (k.request.folder) {
        folderFiles = k.file.folderFiles(k.request.folder);
    }
    else {
        folderFiles = k.file.folderFiles("\\");
    }

    var subfolders = k.file.subFolders(k.request.folder);

    var currentfolder = "\\";

    if (k.request.folder) {
        currentfolder = k.request.folder;
    }
</script>
<div>
    <h4> All files list </h4>
    <table border="1" cellspacing="1" cellpadding="5">
        <tbody>
            <tr><th>FileName</th><th>FullPath</th><th>Size</th><th>Preview</th>
            </tr>
            <tr k-foreach="item allfiles" k-repeat-self="true">
                <td k-content="item.name">name</td>
                <td k-content="item.fullName">fullName</td>
                <td k-content="item.stringSize">size</td>
                <td><a target="_blank" k-href="item.absoluteUrl">Preview</a></td>
            </tr>
        </tbody>
    </table>
</div>
<div>
    <h4> Current Folder :  <span k-content="currentfolder"> folder </span></h4>
    <table border="1" cellspacing="1" cellpadding="5">
        <tbody>
            <tr><th>FileName</th><th>FullPath</th><th>Size</th><th>Del</th><th>Preview</th>            </tr>
            <tr k-foreach="item folderFiles" k-repeat-self="true">
                <td k-content="item.name">name</td>
                <td k-content="item.fullName">name</td>
                <td k-content="item.stringSize">size</td>
                <td><a k-href="appendUrl('deleteFile', item.fullName)"> del </a></td>
                <td><a target="_blank" k-href="item.absoluteUrl">Preview</a></td>
            </tr>
        </tbody>
    </table>
</div>
<div>
    <h4> Sub Folder List</h4>
    <table border="1" cellspacing="1" cellpadding="5">
        <tbody>
            <tr><th>FolderName</th><th>FullPath</th><th>Del</th><th>Go To</th></tr>
            <tr k-foreach="item subfolders" k-repeat-self="true">
                <td k-content="item.name">name</td>
                <td k-content="item.fullName">name</td>
                <td><a k-href="setUrl('deleteFolder', item.fullName)"> del </a></td>
                <td><a k-href="setUrl('Folder', item.fullName)"> go to </a></td>
            </tr>
        </tbody>
    </table>
</div>

<div><br><h2> use below form to upload files </h2></div>
<form method="post" action="" enctype="multipart/form-data">
    <div><label for="file">Choose file</label>
        <input type="file" id="file" name="file" multiple="">
    </div>
    <div><button>upload</button></div>
</form>
<form method="post" action="">
    <div><label for="file">Creat a Folder</label><input type="text" name="createfolder" value="">
    </div>
    <div><button>create</button></div>
</form>  
	]]>   
  </Example>
  <Props />
  <Methods>
    <Method>
      <Name>write</Name>
      <Description>Write the text to the file name. When the target is exist, it will be overwritten.</Description>
      <Example>
        <![CDATA[
         <script engine="kscript">
          k.file.write("folder\\filename.txt", "content to write to text file");
          var info = k.file.write("rootfile.txt", "content to write to text file");
          k.response.write("done");
        </script>
	]]>
      </Example>
      <Params>
        <Param>
          <Name>fileName</Name>
          <Description>the full file name includes folders if any.</Description>
          <Type>string</Type>
        </Param>
        <Param>
          <Name>content</Name>
          <Description>Text content that will be written into the file.</Description>
          <Type>string</Type>
        </Param>
      </Params>
      <ReturnType>FileInfo</ReturnType>
    </Method>
    <Method>
      <Name>append</Name>
      <Description>Write the text to the file name. When the target is NOT exist, it will be created</Description>
      <Example>
        k.file.append("filename.txt", "content to append to text file");
      </Example>
      <Params>
        <Param>
          <Name>fileName</Name>
          <Description>the full file name includes folders if any.</Description>
          <Type>string</Type>
        </Param>
        <Param>
          <Name>content</Name>
          <Description>content</Description>
          <Type>string</Type>
        </Param>
      </Params>
    </Method>
    <Method>
      <Name>read</Name>
      <Description>Read all the text of the file</Description>
      <Example>
        var value = k.file.read("filename.txt");
      </Example>
      <Params>
        <Param>
          <Name>fileName</Name>
          <Description>fileName</Description>
          <Type>string</Type>
        </Param>
      </Params>
      <ReturnType>string</ReturnType>
    </Method>
    <Method>
      <Name>delete</Name>
      <Description>Delete the file</Description>
      <Example>
        k.file.delete("filename.txt");
      </Example>
      <Params>
        <Param>
          <Name>fileName</Name>
          <Description>the full file name includes folders if any.</Description>
          <Type>string</Type>
        </Param>
      </Params>
    </Method>
    <Method>
      <Name>writeBinary</Name>
      <Description>Write an array of bytes to the site disk folder.</Description>
      <Example>
        <![CDATA[
        <script engine="kscript">
          if (k.request.method="POST"){
			  var name  = k.request.form.firstname;
			  if (k.request.files.length>0){
				  var file = k.request.files[0];
				  var info = k.file.writeBinary(file.fileName, file.bytes);
			  }
          }
        </script>
        
        	]]>
      </Example>
      <Params>
        <Param>
          <Name>fileName</Name>
          <Description>the full file name includes folders if any.</Description>
          <Type>string</Type>
        </Param>
        <Param>
          <Name>binary</Name>
          <Description>binary</Description>
          <Type>array</Type>
        </Param>
      </Params>
      <ReturnType>FileInfo</ReturnType>
    </Method>
    <Method>
      <Name>readBinary</Name>
      <Description>read the file into a byte array </Description>
      <Example>
        <![CDATA[
        <script engine="kscript">
        
			var bytes = k.file.readBinary("file.pdf"); 
         
			var info = k.file.writeBinary("newname.pdf", bytes);
			   
        </script>
        
        	]]>
      
      </Example>
      <Params>
        <Param>
          <Name>fileName</Name>
          <Description>fileName</Description>
          <Type>string</Type>
        </Param>
      </Params>
      <ReturnType>Byte[]</ReturnType>
    </Method>
    <Method>
      <Name>exists</Name>
      <Description>Check whether the file exists or not, filename can be:  /folder/filename.txt.</Description>
      <Example>
        <![CDATA[
    <script engine="kscript">
    
    if (k.file.exists("filename.txt"))
    {   var content = k.file.read("filename.txt"); 
        k.response.write(content);  
    }
    else 
    { 
      k.response.write("file not exists");   
    }  
</script>
        
        	]]>
      </Example>
      <Params>
        <Param>
          <Name>fileName</Name>
          <Description>fileName</Description>
          <Type>string</Type>
        </Param>
      </Params>
      <ReturnType>boolean</ReturnType>
    </Method>

    <Method>
      <Name>deleteFolder</Name>
      <Description>Delete a file folder includes all sub directories and files.</Description>
      <Example>
        <![CDATA[
    <script engine="kscript">
    
    if (k.request.deleteFolder) {
        k.file.deleteFolder(k.request.deleteFolder);
    } 
</script> ]]>
      </Example>
      <Params>
        <Param>
          <Name>folder</Name>
          <Description>the full folder path</Description>
          <Type>string</Type>
        </Param>
      </Params>
      <ReturnType>boolean</ReturnType>
    </Method>
     
    <Method>
      <Name>getAllFiles</Name>
      <Description>Return all files in all folders, return an Array of FileInfo</Description>
      <Example>
        <![CDATA[
    <script engine="kscript">  
     var allfiles= k.file.getAllFiles();   
</script> 

<table border="1" cellspacing="1" cellpadding="5">
        <tbody>
            <tr><th>FileName</th><th>FullPath</th><th>Size</th><th>Preview</th>
            </tr>
            <tr k-foreach="item allfiles" k-repeat-self="true">
                <td k-content="item.name">name</td>
                <td k-content="item.fullName">name</td>
                <td k-content="item.stringSize">size</td>
                <td><a target="_blank" k-href="item.absoluteUrl">Preview</a></td>
            </tr>
        </tbody>
    </table> 
]]>   
      </Example>
      <Params />
      <ReturnType>FileInfo</ReturnType>
    </Method>


    <Method>
      <Name>folderFiles</Name>
      <Description>Return all files in the provided folder, return an Array of FileInfo</Description>
      <Example>
        <![CDATA[
    <script engine="kscript">  
    
     var folderFiles;
    if (k.request.folder) {
        folderFiles = k.file.folderFiles(k.request.folder);
    }
    else {
        folderFiles = k.file.folderFiles("\\");
    }
   
</script> 

<table border="1" cellspacing="1" cellpadding="5">
        <tbody>
            <tr><th>FileName</th><th>FullPath</th><th>Size</th><th>Preview</th>
            </tr>
            <tr k-foreach="item folderFiles" k-repeat-self="true">
                <td k-content="item.name">name</td>
                <td k-content="item.fullName">name</td>
                <td k-content="item.stringSize">size</td>
                <td><a target="_blank" k-href="item.absoluteUrl">Preview</a></td>
            </tr>
        </tbody>
    </table> 
]]>
      </Example>
      <Params>
        <Param>
          <Name>folder</Name>
          <Description>Full folder path</Description>
          <Type>string</Type>
        </Param>
      </Params>
      <ReturnType>FileInfo</ReturnType>
    </Method>


    <Method>
      <Name>subFolders</Name>
      <Description>List sub folders under current folder, return an Array of FolderInfo</Description>
      <Example>
        <![CDATA[
  <script engine="kscript">  
        var subfolders = k.file.subFolders(k.request.folder);
  </script> 

<table border="1" cellspacing="1" cellpadding="5">
        <tbody>
            <tr><th>FileName</th><th>FullPath</th><th>Size</th><th>Preview</th>
            </tr>
            <tr k-foreach="item allfiles" k-repeat-self="true">
                <td k-content="item.name">name</td>
                <td k-content="item.fullName">name</td>
                <td k-content="item.stringSize">size</td>
                <td><a target="_blank" k-href="item.absoluteUrl">Preview</a></td>
            </tr>
        </tbody>
    </table> 
]]>
      </Example>
      <Params>
        <Param>
          <Name>folder</Name>
          <Description>Parent Folder</Description>
          <Type>string</Type>
        </Param>
      </Params>
      <ReturnType>FolderInfo</ReturnType>
    </Method> 
   
  <Method>
    <Name>createFolder</Name>
    <Description>create a sub folder under current folder</Description>
    <Example>
      <![CDATA[
  <script engine="kscript">  
      
        if (k.request.createFolder) {
            k.file.createFolder(k.request.createFolder, k.request.folder);
        }
        
  </script> 

<form method="post" action="">
    <div><label for="file">Creat a Folder</label><input type="text" name="createfolder" value="">
    </div>
    <div><button>create</button></div>
</form>
]]>
    </Example>
    <Params>
      <Param>
        <Name>newFolderName</Name>
        <Description>Name of new folder</Description>
        <Type>string</Type>
      </Param>
      <Param>
        <Name>parentFolder</Name>
        <Description>the parent folder, can be ""</Description>
        <Type>string</Type>
      </Param>
    </Params>
    <ReturnType></ReturnType>
   </Method>


    <Method>
      <Name>deleteFolder</Name>
      <Description>Delete a folder and all sub directories and files in it.</Description>
      <Example>
        <![CDATA[
  <script engine="kscript">  
      
      if (k.request.deleteFolder) {
        k.file.deleteFolder(k.request.deleteFolder);
    }
        
  </script> 
 
]]>
      </Example>
      <Params> 
        <Param>
          <Name>folderName</Name>
          <Description>Full folder name</Description>
          <Type>string</Type>
        </Param>
      </Params>
      <ReturnType></ReturnType>
    </Method>
     
  </Methods>
 
</KScriptSetting>