Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Image upload using jqmobile & phonegap

Posted on 2011-09-27
1
Medium Priority
?
5,669 Views
Last Modified: 2012-05-12
Hi Experts,

I am having major problems, and having searched the net cannot find an example or tutorial that I can get working with my code.

I am writing an app for IOS & Android using dreamweaver 5.5 / jqmobile / phonegap. In one of the forms, I need to be able to upload photo's. I have it working that you can select an image from the gallery or take a new photo and it appears in the form.

What I need to happen is that the image is sent to the webserver, and this is where I am well and truly stuck.

Here is the JS
<script type="text/javascript" charset="utf-8">
    var pictureSource;   // picture source
    var destinationType; // sets the format of returned value 
 
    // Wait for PhoneGap to connect with the device
    function onLoad() {
        document.addEventListener("deviceready",onDeviceReady,false);
    }
 
    // PhoneGap is ready to be used!
    function onDeviceReady() {
        pictureSource=navigator.camera.PictureSourceType;
        destinationType=navigator.camera.DestinationType;
    }
	function getPicture(sourceType)
    {
        var options = { quality: 10 };
        if (sourceType != undefined) {
            options["sourceType"] = sourceType;

        }
        // if no sourceType specified, the default is CAMERA 
        navigator.camera.getPicture(getPicture_Success, null, options);
    };

    function getPicture_Success(imageData)
    {
            //alert("getpic success");
            document.getElementById("smallImage").src = "data:image/jpeg;base64," + imageData;

    }   
</script>

Open in new window


The part of the form that calls the camera function and then displays a thumbnail

      <div data-role="collapsible" data-collapsed="true">
        <h3>Photographs</h3>
    
        <p>Please upload your photographs</p>
        <div data-role="controlgroup" data-type="horizontal"><a href="#" data-role="button" data-icon="plus" data-iconpos="right" onclick="getPicture(pictureSource.PHOTOLIBRARY);">Gallery Photo</a><a href="#" data-role="button" data-icon="plus" data-iconpos="right" onclick="getPicture();">New Photo</a>

    <img style="width:60px;height:60px;" id="smallImage" src="" />
        </div>
      </div>

Open in new window


So its all working to this point.

Now I am stuck!

I was thinking add to the JS something along the lines of

    function getPicture_Success(imageData)
    {
            //alert("getpic success");
            document.getElementById("smallImage").src = "data:image/jpeg;base64," + imageData;
			var imageName =  "data:image/jpeg;base64," + imageData;
        	$.ajax({  
            type: "POST",  
            url: "http://quote-sys.co.uk/ClaimApp/upload.php",  
            data: "image="+ imageName,  
            success: function(){  
					$('div.successUpload').fadeIn();
              }  
        }); 
    }  

Open in new window


Would that work and if so what would I need to put in the upload.php file to retrieve the image?
If not can someone possibly give me a working example of how to do it?

Ideally, I want to name/number the images sequentially so that server side when the claim form is uploaded the script can then scan the directory and pickup the correct images to store in the DB with the rest of the form.

Thanks for reading
Jim

0
Comment
Question by:dovercomputers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
dovercomputers earned 0 total points
ID: 36709422
Solved it myself. For those that are interested the fully working solution is below.

JS
<script type="text/javascript">
$(document).ready(function() {
	function sendImage(src) {
		
		// Set the image source [library || camera]
		src = (src == 'library') ? Camera.PictureSourceType.PHOTOLIBRARY : Camera.PictureSourceType.CAMERA;
		
		// Aquire the image -> Phonegap API
		navigator.camera.getPicture(success, fail, {quality: 10, sourceType: src});
		
		// Successfully aquired image data -> base64 encoded string of the image file
		function success(imageData) {
			var url = 'http://your server/upload.php';
			var params = {image: imageData};
			
			// send the data
			$.post(url, params, function(data) {
				
				// Display the selected image on send complete
				$('#image').attr('src', 'data:image/jpeg;base64,' + params['image']);
				
			});
		}
		
		function fail(message) { alert(message); }
	}
	
	$('.send-image').click(function () { sendImage($(this).val()); }); 
});

</script>

Open in new window


HTML
<input type="button" class="send-image" value="camera" /><input type="button" class="send-image" value="library" /><img style="width:60px;height:60px;" id="image" src="" />

Open in new window


PHP
<?php
if ($_REQUEST['image']) {
	
	// convert the image data from base64
	$imgData = base64_decode($_REQUEST['image']);
	
	// set the image paths
	$file = '/your server path here/upload/' . md5(date('Ymdgisu')) . '.jpg';
	$url = 'http://your URL here/upload/' . $file; 
	
	// delete the image if it already exists
	if (file_exists($file)) { unlink($file); }
	
	// write the imgData to the file
	$fp = fopen($file, 'w');
	fwrite($fp, $imgData);
	fclose($fp);
}
?>

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question