2013-03-19 47 views
2

我想在Phonegap應用程序中捕捉圖像,然後使用$發送。 ajax方法將其發送到具有Web服務的遠程服務器。淨。使用PhoneGap,ajax和web service.net將圖像上傳到遠程服務器

我不能使用「upload」方法發送到服務器,因爲它不接受uri .asmx 我需要一個方法$。阿賈克斯後。 我使用Web服務:

[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; 
} 
+0

**你有什麼特別提示d?**你的代碼在哪裏? – Jimbo 2013-03-19 14:50:21

+0

@ user2174280如果您發現它是正確和有用的,您應該接受該答案。 – 2013-06-25 08:57:33

回答

0

您應該使用PhoneGap的

提供 CameraFileUploadOptions對象

您的代碼將是這個樣子

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); 

而且你的Web服務的方法應該是這樣的:

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

謝謝你的回答。 – user2174280 2013-03-19 15:50:17

相關問題