gpt4 book ai didi

jquery - 使用 PhoneGap、ajax 和 web service.net 将图像上传到远程服务器

转载 作者:行者123 更新时间:2023-12-01 05:56:06 24 4
gpt4 key购买 nike

我想在 Phonegap 应用程序中捕获图像,然后使用 $. ajax 方法通过 Web 服务将其发送到远程服务器。净。

我无法使用“上传”方法发送到服务器,因为它不接受 uri .asmx我需要一个方法 $. Ajax 帖子。我使用网络服务:

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

最佳答案

您应该使用CameraFileUploadOptions Phonegap 提供的对象

你的代码看起来像这样

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

您的网络服务方法应如下所示:

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

关于jquery - 使用 PhoneGap、ajax 和 web service.net 将图像上传到远程服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15502344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com