![](/img/trans.png)
[英]Jquery Ajax method error with asp.net web service on a remote server subdomain
[英]upload the image to the remote server with PhoneGap, ajax and web service.net
我想在Phonegap應用程序中捕獲圖像,然后使用$發送。 使用Web服務將其發送到遠程服務器的ajax方法。 凈。
我無法使用“上傳”方法發送到服務器,因為它不接受uri .asmx,我需要使用方法$。 阿賈克斯郵報。 我使用網絡服務:
[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;
}
您應該使用Phonegap提供的Camera和FileUploadOptions對象
您的代碼看起來像這樣
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.