简体   繁体   中英

upload the image to the remote server with PhoneGap, ajax and web service.net

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 can't use the method "upload" for sending to the server because it does not accept the uri .asmx I need a method $. ajax post. 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;
}

You should use Camera and FileUploadOptions objects provided by Phonegap

Your code would look something like this

document.addEventListener("deviceready", function() {

    var cameraParams = { 
        quality : 20,
        destinationType: Camera.DestinationType.FILE_URI,
        correctOrientation: true
    };
    navigator.camera.getPicture(onPhotoTakenSuccess, function() {}, cameraParams);

    var onPhotoTakenSuccess = function(imageUri) {

        var url = "http://yourserviceurl/service.asmx/Upload";

        var params = new Object();
        params.otherinfo = "whatever";  //you can send additional info with the file

        var options = new FileUploadOptions();
        options.fileKey = "file";
        options.fileName = imageUri.substr(imageUri.lastIndexOf('/')+1);
        options.mimeType = "image/jpeg";
        options.params = params;
        options.chunkedMode = false;

        var ft = new FileTransfer();
        ft.upload(imageUri, url, successCallback, errorCallback, options);
    };


}, false);

And your webservice method should look like:

[WebMethod]
public void Upload()
{
    var file = Request.Files[0];
    string otherInfo = Request["otherinfo"];
    //do whatever you want to do with the file now
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM