blob: 753eb3ade4a2c778b92b48abdba30f09e7649e3a [file] [log] [blame]
<!DOCTYPE html>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<html>
<head>
<!-- meta name="viewport" content="width=device-width, height=device-height, user-scalable=yes, initial-scale=2.0, maximum-scale=4.0, minimum-scale=1.0" / -->
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/> <!-- ISO-8859-1 -->
<title>Cordova</title>
<link rel="stylesheet" href="master.css" type="text/css" media="screen"/>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
var deviceReady = false;
var persistent_root;
var getFileSystemRoot = (function () {
var onError = function (error) {
console.log('unable to retrieve file system: ' + error.code);
};
// one-time retrieval of the root file system entry
if (typeof persistent_root === 'undefined') {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function (fileSystem) {
persistent_root = fileSystem.root;
}, onError);
}
// public function returns private root entry
return persistent_root;
});
/**
* Gets file or directory
*/
function getEntry(isFile, onSuccess, options) {
var root = getFileSystemRoot();
//TODO
console.log(typeof root);
var filePath = document.getElementById('filePath');
var onGetEntryError = function (fileError) {
console.log("Error creating/opening file/directory: " + fileError.code);
document.getElementById('file_status').innerHTML = "Error creating/opening file/directory: " + fileError.code;
};
if (isFile == true) {
console.log('Creating file');
root.getFile(
filePath.value,
options,
onSuccess,
onGetEntryError);
} else {
console.log('Creating directory');
root.getDirectory(
filePath.value,
options,
onSuccess,
onGetEntryError);
}
};
/**
* Creates or opens file/directory
*/
function createOrOpen(isFile) {
var onGetEntrySuccess = function (fileEntry) {
document.getElementById('file_status').innerHTML = "isFile: " + fileEntry.isFile + "; isDirectory: " + fileEntry.isDirectory + "; name: " + fileEntry.name + "; path: " + fileEntry.fullPath;
entry = fileEntry;
};
getEntry(isFile, onGetEntrySuccess, {create: true, exclusive: true});
};
/**
* Check if file/directory exists
*/
function isExists(isFile) {
console.log("Start checking...");
var root = getFileSystemRoot();
var onError = function (fileError) {
console.log("Error checking file/directory: " + fileError.code);
document.getElementById('file_status').innerHTML = "Error checking file/directory: " + fileError.code;
};
var onSuccess = function (isExist) {
document.getElementById('file_status').innerHTML = "is exists: " + isExist;
};
var filePath = document.getElementById('filePath');
if (isFile == true) {
console.log('check if file exists');
navigator.fileMgr.testFileExists(
filePath.value,
onSuccess,
onError);
} else {
navigator.fileMgr.testDirectoryExists(
filePath.value,
onSuccess,
onError);
}
};
/**
* Remove file or directory
*/
function remove(isFile) {
var onSuccess = function (fileEntry) {
var onRemoveError = function (fileError) {
console.log("Error removing file/directory: " + fileError.code);
document.getElementById('file_status').innerHTML = "Error removing file/directory: " + fileError.code;
};
var onRemoveSuccess = function () {
document.getElementById('file_status').innerHTML = "file/directory was removed";
};
fileEntry.remove(onRemoveSuccess, onRemoveError);
};
getEntry(isFile, onSuccess, {create: false});
};
/**
* Writes data in file
*/
function writeInFile() {
var onSuccess = function (fileEntry) {
var writer = new FileWriter(fileEntry);
writer.onwrite = function (evt) {
console.log("End writing...");
document.getElementById('file_status').innerHTML = writer.length + " bytes was written";
};
writer.onerror = function (evt) {
console.log("writing error");
document.getElementById('file_status').innerHTML = "writing error: " + evt.target.error;
};
writer.write(textToWrite.value);
console.log("End writing");
};
console.log("Start getting entry");
getEntry(true, onSuccess, { create: false });
};
/**
* Reads file as base64 string
*/
function readAsBase64() {
var onSuccess = function (fileEntry) {
var reader = new FileReader();
reader.onloadend = function (evt) {
console.log("read success");
document.getElementById('file_status').innerHTML = evt.target.result;
};
reader.onerror = function (evt) {
console.log("read error");
document.getElementById('file_status').innerHTML = "read error: " + evt.target.error;
};
reader.readAsDataURL(fileEntry);
};
console.log("Start getting entry");
getEntry(true, onSuccess, { create: false });
};
/**
* Uploads selected file to specified server
*/
function uploadFile() {
var onSuccess = function (fileUploadResult) {
console.log("Upload success");
document.getElementById('file_status').innerHTML = "Code: " + fileUploadResult.responseCode + "; Sent = " + fileUploadResult.bytesSent + "; Response: " + fileUploadResult.response;
};
var onError = function (error) {
document.getElementById('file_status').innerHTML = "Upload error: " + error.code;
};
console.log("Set upload parameters");
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = "newFile.txt";
options.mimeType = "text/plain";
var filePath = document.getElementById('filePath').value;
var server = document.getElementById('server').value;
var params = new Object();
params.key1 = "value1";
params.key2 = "value2";
options.params = params;
var fileTransfer = new FileTransfer();
fileTransfer.upload(filePath, server, onSuccess, onError, options);
};
/**
* Function called when page has finished loading.
*/
function init() {
document.addEventListener("deviceready", function () {
deviceReady = true;
console.log("Device=" + device.platform + " " + device.version);
getFileSystemRoot();
}, false);
window.setTimeout(function () {
if (!deviceReady) {
alert("Error: Cordova did not initialize. Demo will not run correctly.");
}
}, 1000);
}
</script>
</head>
<body onLoad="init();" id="stage" class="theme">
<h1>File</h1>
<div id="info">
<span class='tb-label'>Enter file/directory name here:</span> <span id="Span1"></span>
<input type="text" id="filePath" style="width:250px;height:20px;"/>
<br/>
<span class='tb-label'>Enter text to write in file :</span> <span id="Span2"></span>
<input type="text" id="textToWrite" style="width:250px;height:20px;"/>
<br/>
<span class='tb-label'>Enter URL to upload file to: </span> <span id="Span3"></span>
<input type="text" id="server" value='http://posttestserver.com/post.php?dir=cordovaTest' style="width:250px;height:20px;"/>
<br/>
Result: <span id="file_status"></span>
</div>
<h2>Action</h2>
<a href="#" class="btn large" onclick="createOrOpen(true);">Create/Open file</a>
<a href="#" class="btn large" onclick="createOrOpen(false);">Create/Open directory</a>
<a href="#" class="btn large" onclick="isExists(true);">Check if file exists</a>
<a href="#" class="btn large" onclick="isExists(false);">Check if directory exists</a>
<a href="#" class="btn large" onclick="remove(true);">Remove file</a>
<a href="#" class="btn large" onclick="remove(false);">Remove directory</a>
<a href="#" class="btn large" onclick="writeInFile();">Write in file (type text in the second field)</a>
<a href="#" class="btn large" onclick="readAsBase64();">Read file as base64</a>
<a href="#" class="btn large" onclick="uploadFile();">Upload file</a>
<h2> </h2><a href="index.html" class="backBtn">Back</a>
</body>
</html>