Since we dont have file structure in iPhone, I am not sure how to implement it in iPhone.
1. Client(device) side implementation
PhoneGap provides file transfer API to transfer file to server, PHP is recommended for server implementation.
function uploadFile() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
var directoryReader = fs.root.createReader();
directoryReader.readEntries(function(entries) {
imageURI=entries[0].toURI(); // Selected File path
var options = new FileUploadOptions();
options.chunkedMode = false;
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
alert(options.fileName);
options.mimeType="text/plain";
var params = new Object();
params.value1 = "test";
params.value2 = "param";
options.params = params;
var ft = new FileTransfer();
ft.upload(imageURI, "ServerIP/Upload.php", win, fail, options,true);
}, function (error) {
alert(error.code);
})}, function (error) {
alert(error.code);
});
}
function win(r) {
alert("success");
alert("Sent = " + r.bytesSent);
}
function fail(error) {
alert("error");
switch (error.code) {
case FileTransferError.FILE_NOT_FOUND_ERR:
alert("Photo file not found");
break;
case FileTransferError.INVALID_URL_ERR:
alert("Bad Photo URL");
break;
case FileTransferError.CONNECTION_ERR:
alert("Connection error");
break;
}
alert("An error has occurred: Code = " + error.code);
}
2. Server side implementation
Create a php page(Upload.php) and host it in IIS
<?php
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "";
echo "Type: " . $_FILES["file"]["type"] . "";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "";
if (file_exists("D:/PHPUPLOAD/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "C:/PHPUPLOAD/" . $_FILES["file"]["name"]); //Save location
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
?>
For example: Create a php file(Upload.php) at – C:\inetpub\wwwroot
No comments:
Post a Comment