Tuesday, March 26, 2013

Phonegap file uploader in android


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>File Transfer Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script>
    <script type="text/javascript" charset="utf-8">

        // Wait for PhoneGap to load
        //
        // document.addEventListener("deviceready", onDeviceReady, false);
  function imageObject(){
        var options,
        fileEntry,
        ft,
        imageURI        
        };
       
        imageObject.prototype = {
        // 기본 셋팅
        initUpload : function(imageURI){
        console.log("initUpload");
        image.options = new FileUploadOptions();
        window.resolveLocalFileSystemURI(imageURI, image.onSuccessFile, image.onFailFile);
        image.imageURI = imageURI;
       
        image.options.fileKey="file";
            image.options.fileName=image.fileEntry.name;          
            image.options.mimeType="image/jpeg";
        image.ft = new FileTransfer();
        },
        // 파일전송
        send : function(){
        image.ft.upload(image.imageURI, "http://203.233.82.229/non/test/file.json", win, fail, image.options);
        },
        onSuccessFile : function(f){
        console.log("getFile");
        image.fileEntry = f;
        alert(f.name);
        },
        onFailFile : function(f){
       
        }
        }
        var image = new imageObject();
       

        // PhoneGap is ready
        //
        function readImageFile() {

            // Retrieve image file location from specified source
            navigator.camera.getPicture(image.initUpload,
                                        function(message) { alert('get picture failed'); },
                                        { quality: 50,
                                        destinationType: navigator.camera.DestinationType.FILE_URI,
                                        sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY,
                                        mediaType: Camera.MediaType.ALLMEDIA }
                                        );

        };
       
        function win(r) {
            console.log("Code = " + r.responseCode);
            console.log("Response = " + r.response);
            console.log("Sent = " + r.bytesSent);
            alert("status : " + JSON.parse(r.response).status);
        }

        function fail(error) {
            alert("An error has occurred: Code = " = error.code);
        }    
     

        </script>
</head>
<body>
    <h1>Example</h1>
    <p>With PhoneGap API</p>
    <button onclick="readImageFile()">Open Image</button>
    <button onclick="image.send()">Send To Server</button>
    <br>
    <br>
    <p>With Original Form</p>
    <form method='POST' enctype='multipart/form-data' action='http://203.233.82.229/non/test/file.json'>
File to upload: <input type="file" name="file"><br>
Notes about the file: <input type="text" name="note"><br>
<br>
<input type="submit" value="Press"> to upload the file!
</form>
   
</body>
</html>

No comments:

Post a Comment