图像上传到远程服务器的PhoneGap,Ajax和Web service.net图像、服务器、PhoneGap、Ajax

2023-09-03 21:53:57 作者:阳光伊人

我要拍摄图像中的PhoneGap的应用程序,然后我通过发送$。 AJAX方法将其发送到远程服务器Web服务。净。

我不能用于发送到服务器的方法上载,因为它不接受的URI的.asmx 我需要一种方法$。 ajax的职位。 我用的是Web服务:

  [WebMethod的]
公共BOOL SavePhoto(GUID标识prestation中GUID IdPhoto,byte []的ImgIn)
{
    System.IO.MemoryStream毫秒​​=新System.IO.MemoryStream(ImgIn);
    System.Drawing.Bitmap B =(System.Drawing.Bitmap)System.Drawing.Image.FromStream(毫秒);
    //思乐曲目n'existe PAS alors上乐CREE
    //如果(!RepertoirePhotoExist(同上prestation))
    // {
           System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(照片/+编号prestation.ToString()));
    //}
    字符串strFichier = HttpContext.Current.Server.MapPath(照片/+编号prestation.ToString()+/+ IdPhoto.ToString()+.JPG);
    //思乐fichier existe alors
    如果(System.IO.File.Exists(strFichier))
    {
        System.IO.File.Delete(strFichier);
    }
    其他
    {
        b.Save(strFichier,System.Drawing.Imaging.ImageFormat.Jpeg);
    }
        返回true;
}
 

解决方案

您应该使用的相机和 FileUploadOptions 的对象

Android上使用camera拍照,把获取的照片上传到远程服务器

您code会是这个样子

  document.addEventListener(deviceready,函数(){

    VAR cameraParams = {
        品质:20,
        destinationType:Camera.DestinationType.FILE_URI,
        correctOrientation:真
    };
    navigator.camera.getPicture(onPhotoTakenSuccess,函数(){},cameraParams);

    VAR onPhotoTakenSuccess =功能(imageUri){

        VAR URL =HTTP://yourserviceurl/service.asmx/Uplo​​ad;

        VAR PARAMS =新的对象();
        params.otherinfo =无所谓; //您可以发送更多的信息与文件

        VAR的选择=新FileUploadOptions();
        options.fileKey =文件;
        options.fileName = imageUri.substr(imageUri.lastIndexOf('/')+ 1);
        options.mimeType =为image / jpeg;
        options.params = PARAMS;
        options.chunkedMode = FALSE;

        VAR英尺=新的文件传输();
        ft.upload(imageUri,网址,successCallback,errorCallback,期权);
    };


}, 假);
 

和你的web服务的方法应该是这样的:

  [WebMethod的]
公共无效上传()
{
    var文件= Request.Files [0];
    字符串=活动促销请求[活动促销];
    //做任何你想现在做的文件
}
 

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
}