Welcome Guest, Not a member yet? Register   Sign In
uploading Photo from PhoneGap
#1

[eluser]RMinor[/eluser]
I have a mobile app written with jQM and PhoneGap that I am uploading a file to a CodeIgniter 2.1.3 website. When I click the Upload button I get an error returned from my upload method that says "You did not select a file to upload". This is my first time uploading a photo from a mobile device to a server so there might be something I am overlooking. Below is my code.

Controller:
Code:
/**
* Method to upload a photo from a mobile device
*/
public function upload_photo() {
    $this->load->model('photo_model');
    $photo = $_FILES['file']['name'];
    if ($this->_upload($photo, $this->input->post('album_id'))) {
        $data['response'] = 'true';
    } else {
        $data['response'] = 'false';
        $data['errors'] = $this->_photo_errors;
    }
    $this->output->set_content_type('application/json')->set_output(json_encode($data));
}

/**
* Method to upload and save photo
* @param string $photo
* @return boolean
*/
protected function _upload($photo, $album_id) {
    if (!$user_id = $this->photo_model->getUserIdFromAlbumId($album_id)) {
        return false;
    }
    $this->load->library('upload');
    $config['upload_path'] = './media/photos/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size'] = '10240';
    $config['remove_spaces'] = true;
    $this->upload->initialize($config);
    if (!$this->upload->do_upload($photo)) {
        $this->_photo_errors['upload'] = $this->upload->display_errors();
    } else {
        $photo_info = $this->upload->data();
        $this->load->library('image_lib');
        $config['source_image'] = $photo_info['full_path'];
        $config['maintain_ratio'] = true;
        $config['new_image'] = './media/photos/resized/' . $photo_info['file_name'];  
        if ($photo_info['image_width'] < 600) {
            $config['width'] = $photo_info['image_width'];
        } else {
            $config['width'] = 600;
        }
        if ($photo_info['image_height'] < 600) {
            $config['height'] = $photo_info['image_height'];
        } else {
            $config['height'] = 600;
        }
        $this->image_lib->initialize($config);
        if ($this->image_lib->resize()) {
            $this->_photo_array['resized'] = $photo_info['file_name'];
            $this->image_lib->clear();
        } else {
            $this->_photo_errors['resize'] = $this->image_lib->display_errors();
        }
        $config['source_image'] = $photo_info['full_path'];
        $config['maintain_ratio'] = true;
        $config['new_image'] = './media/photos/' . $photo_info['raw_name'] . '_200' . $photo_info['file_ext'];
        if ($photo_info['image_width'] > $photo_info['image_height']) {
            $config['height'] = 200;
        } else {
            $config['width'] = 200;
        }
        $this->image_lib->initialize($config);
        if ($this->image_lib->resize()) {
            $config['source_image'] = './media/photos/' . $photo_info['raw_name'] . '_200' . $photo_info['file_ext'];
            $config['maintain_ratio'] = FALSE;
            $config['new_image'] = './media/photos/thumbnail/' . $photo_info['raw_name'] . '_thumb' . $photo_info['file_ext'];
            $config['width'] = 200;
            $config['height'] = 200;
            if ($photo_info['image_width'] > $photo_info['image_height']) {
                $config['x_axis'] = (($photo_info['image_width'] / 2) - ($config['width'] / 2));
                $config['y_axis'] = 0;
            } else {
                $config['x_axis'] = 0;
                $config['y_axis'] = (($photo_info['image_height'] / 2) - ($config['height'] / 2));
            }
            $this->image_lib->initialize($config);
            if ($this->image_lib->crop()) {
                $this->_photo_array['thumbnail'] = $photo_info['raw_name'] . '_thumb' . $photo_info['file_ext'];
                $caption = 'Uploaded via mobile.';
                if ($this->photo_model->create($this->_photo_array['resized'], $this->_photo_array['thumbnail'], $caption, 0, $album_id)) {
                    $path = './media/photos/' . $photo_info['raw_name'] . '_200' . $photo_info['file_ext'];
                    unlink($path);
                    $path = $photo_info['full_path'];
                    unlink($path);
                    $this->image_lib->clear();
                    return true;
                } else {
                    $path = './media/photos/' . $photo_info['raw_name'] . '_200' . $photo_info['file_ext'];
                    unlink($path);
                    $path = $photo_info['full_path'];
                    unlink($path);
                    $this->image_lib->clear();
                    return false;
                }
            } else {
                $this->_photo_errors['thumbnail'] = $this->image_lib->display_errors();
            }
        } else {
            $this->_photo_errors['200'] = $this->image_lib->display_errors();
        }
        return false;
    }
}

Mobile Code:
Code:
function uploadPhoto() {
    $("#upload_loader").show();
    var options = new FileUploadOptions();
    options.fileKey = "file";
    options.fileName = selectedImageURI.substr(selectedImageURI.lastIndexOf('/')+1);
    options.mimeType = "image/jpeg";
    var params = new Object();
    params.album_id = localStorage.getItem('album_id');
    options.params = params;
    options.chunkedMode = false;
    var ft = new FileTransfer();
    ft.upload(selectedImageURI, encodeURI("http://wedding.minortechnologies.com/mobile/upload-photo"), win, fail, options);
}
  
function fail(error) {
    $("#upload_loader").hide();
    alert("An error has occurred: Code = " + error.code);
}
  
function win(r) {
    $("#upload_loader").hide();
    console.log("Code = " + r.responseCode);
    console.log("Response = " + r.response);
    console.log("Sent = " + r.bytesSent);
    alert(r.response);
}




Theme © iAndrew 2016 - Forum software by © MyBB