[英]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 $. 我想在Phonegap应用程序中捕获图像,然后使用$发送。 ajax method to send it to a remote server with web service.
使用Web服务将其发送到远程服务器的ajax方法。 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 $. 我无法使用“上传”方法发送到服务器,因为它不接受uri .asmx,我需要使用方法$。 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 您应该使用Phonegap提供的Camera和FileUploadOptions对象
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: 您的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.