Saving image Phonegap + jquerymobile + asp .net

Posted: February 8, 2012 in web

html – jquery

....
	$('#contactform').submit(function(e){
			$('.submit').closest('.ui-btn').hide();
			var url = 'test.aspx';
			var params = {image: $('#imgcam').val(), Title: $('#emailAddress').val(), Name: $('#contactName').val()};

			//alert('submit');
			$('#maincontent').html('<img src="images/ajax-loader.gif" /><p>Sending data...Please wait.</p>');
						

			//send the data
			$.post(url, params, function(data) {
							
            				$('#maincontent').html('Info submitted...Thank you!');
				 
			});




	return false;
		
	});
.....	

test.aspx – code behind (file upload or via image stream)

...
 if (HttpContext.Current.Request.Files["file"] != null)
        {

            string baseImageLocation = Server.MapPath(@"images\temp\");
            HttpFileCollection uploads = HttpContext.Current.Request.Files;
            //Guid g = Guid.NewGuid();
            HttpPostedFile file2 = uploads["file"];
            string fileExt = Path.GetExtension(file2.FileName).ToLower();
            string fileName = Path.GetFileName(file2.FileName);
            if (fileName != "")
            {
                if (fileExt == ".jpg" || fileExt == ".gif")
                    file2.SaveAs(baseImageLocation + g + fileExt);

            }
            
          //upload to directory
            //System.IO.MemoryStream ms = new System.IO.MemoryStream();
            //string b64 = HttpContext.Current.Request.Form["image"];
            //byte[] b;
            //b = Convert.FromBase64String(b64);
            //ms = new System.IO.MemoryStream(b);

            //System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
            //string baseImageLocation = Server.MapPath(@"images\temp\");
            //img.Save(baseImageLocation + g + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            //img.Dispose();
          // eof directory upload
            //ms.Close();

        }
...
Advertisements
Comments
  1. j says:

    I want to capture an image in a Phonegap app, then I send using the $. ajax method to send it to a remote server with web service. net.

    I use the web service:

    [WebMethod]
    public bool SavePhoto(Guid IdPrestation, Guid IdPhoto, byte[] ImgIn)
    {
    System.IO.MemoryStream ms = new System.IO.MemoryStream(ImgIn);
    System.Drawing.Bitmap b =(System.Drawing.Bitmap)System.Drawing.Image.FromStream(ms);
    //Si le repertoire n’existe pas alors on le crée
    // if (! RepertoirePhotoExist(IdPrestation))
    //{
    System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(“Photos/” + IdPrestation.ToString()));
    //}
    string strFichier = HttpContext.Current.Server.MapPath(“Photos/” + IdPrestation.ToString() + “/” + IdPhoto.ToString() + “.jpg”);
    // Si le fichier existe alors
    if (System.IO.File.Exists(strFichier))
    {
    System.IO.File.Delete(strFichier);
    }
    else
    {
    b.Save(strFichier, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    return true;
    }

    and Phonegap

    PhoneGap

    function onBodyLoad() {

    document.addEventListener(“deviceready”,onDeviceReady,false);

    }

    $(document).ready(function(){

    $(document).bind(‘deviceready’, function(){

    function sendImage(src) {

    src = (src == ‘library’) ? Camera.PictureSourceType.PHOTOLIBRARY : Camera.PictureSourceType.CAMERA;

    alert(“src” +src);

    navigator.camera.getPicture(success, fail, {quality: 45, sourceType: src});

    function success(imageData) {

    //var params = {image: imageData};

    // send the data

    IdPrestation = “3d660013-3028-46c3-adfa-d7141a712ed7”;

    IdPhoto = “3a660013-3028-46c3-adfa-d7141a712ed7″;

    var ImgIn= imageData;

    var ln=””;

    ln =IdPrestation+”;”+IdPhoto+”;”+ImgIn;

    //var params = {image: imageData};

    //$(‘#u_image’).attr(‘src’, ‘data:image/jpeg;base64,’ + imageData);

    // $(‘#image’).val(imageData); //image info do not add base64 or it will be unreadable when uploaded

    // send the data (NOTICE that I trigger the submit event here rather than binding code to the submit event)

    alert(‘uploading1’);

    $.ajax({

    alert(‘uploading12’);

    type: ‘POST’,

    url: ‘uri/Service1.asmx/SavePhoto’,

    data: “{ ‘ln’:'”+ln+”‘ }”,
    dataType: ‘json’,

    crossDomain:true,

    cache:false,

    success: function(response) {

    if(response == 1) {

    alert(“sucsee”);

    }//END SUCCESS

    else {

    alert(‘There was an error. Please Try again’);

    }

    }

    });//ENDS THE AJAX CALL

    }

    }

    function fail(message) { alert(message); }

    $(‘.send-image’).click(function () { sendImage($(this).val()); });

    });

    });

    Upload an image

    The application does not capture an image. Already in the error in logcat is ajax request. Please help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s