2 Replies Latest reply on Oct 16, 2017 6:30 PM by rupakd64397021

    Image upload in app folder

    rupakd64397021

      Hi,

      I am using the following code to upload a picture in local folder. It works fine when i use server path for file uploading. But fails when using the localhost path. It returns 'error :3'. My intention is to store the image in local folder and use it in future while synchronizing online.

      Using npm: '3.10.10' cordova: '7.0.1'

       

      ----------------

      Javascript:

      ---------------

      $(document).ready(function() {  
        $
      ('#getPhoto').click(function(e) {
        sessionStorage
      .removeItem('imagepath');  
        navigator
      .camera.getPicture(onSuccess, onFail, {
        quality
      : 50,
        destinationType
      : Camera.DestinationType.FILE_URI
        
      //sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
        
      });
        
      });
        
      });

        
      function uploadImage(imageURI){
        
      //var serverURL = encodeURI("abc.com/xxprojects/upload/upload.php");
        
      var serverURL  = encodeURI("localhost/xxprojects/upload.php");
        
      var options  = new FileUploadOptions();
        options
      .fileKey  = "file";
        options
      .fileName  = imageURI.substr(imageURI.lastIndexOf('/')+1);
        options
      .mimeType  = "image/jpeg";
        options
      .chunkedMode  = false;
        options
      .headers = {Connection: "close"};
        
      var ft = new FileTransfer();
        ft
      .upload(imageURI, serverURL, onUploadSuccess, onUploadError, options);
        
      }


        
      function onUploadSuccess(){
        alert
      ('Image Upload Successfully');
        
      }

        
      function onUploadError(){
        alert
      ('Error uploading!!');
        
      }

        
      function onSuccess(imageURI) {
        
      var image = document.getElementById('image');
        image
      .style.display = 'block';
        image
      .src = imageURI;
        
      //alert(imageURI);
        uploadImage
      (imageURI);  
        
      }




        
      function onFail(message) {
        alert
      ('Failed because: ' + message);
        
      }

       

      ---------

      HTML

      ---------

      <div data-role="page" id="cameraPage" data-theme="b">
        
      <div data-role="header" data-position="fixed">
        
      <a href="#" class="ui-btn-left" data-rel="back"></a>
        
      <h1>Camera</h1>
        
      </div>
        
      <div role="main" class="ui-content">
        
      <img style="display:block;width:80px;height:80px;" id="image" src="" />
        
      <br />
        
      <input id="getPhoto" type="button" value="Camera">
        
      </div>
        
      </div>

       

      -------

      PHP

      -------

      <?php
      //Allow Headers
      header
      ('Access-Control-Allow-Origin: *');

      // Directory where uploaded images are saved
      $dirname
      = "upload/";
      // If uploading file
      if ($_FILES) {
        print_r
      ($_FILES);
        mkdir
      ($dirname, 0777, true);
        move_uploaded_file
      ($_FILES["file"]["tmp_name"],$dirname."/".$_FILES["file"]["name"]);
      }

      ?>