Cordova Phonegap文件传输上传图像到Webservice
我需要使用phonegaps摄像头和文件传输API将图像上传到Web服务 例如,在Web服务上:Cordova Phonegap文件传输上传图像到Webservice,cordova,upload,Cordova,Upload,我需要使用phonegaps摄像头和文件传输API将图像上传到Web服务 例如,在Web服务上: [WebMethod] [ScriptMethod(UseHttpGet = true)] public string SaveImage(string imageData, string filename) { string success = "Nothing"; try
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string SaveImage(string imageData, string filename)
{
string success = "Nothing";
try
{
string filename = "text.png";
if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("~/devimg")))
{
System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/devimg/"));
}
string path = HttpContext.Current.Server.MapPath("~/devimg/").ToString();
byte[] imgByte = Convert.FromBase64String(imageData);
using (var imgStream = new MemoryStream(imgByte, true))
{
Bitmap img = new Bitmap(imgStream);
Image i = (Image)img;
i.Save(path + "" + filename, ImageFormat.Png);
success = "Image created!!";
}
}
catch (Exception e)
{
success = e.ToString();
}
return success;
}
并且在应用程序中有:
navigator.camera.getPicture( function( imgdata ){
//Base 64 encoded image file or whatever
}, CameraError , {
quality: 50,
destinationType: navigator.camera.DestinationType.DATA_URL,
sourceType: navigator.camera.PictureSourceType.CAMERA,
encodingType: navigator.camera.EncodingType.JPEG,
saveToPhotoAlbum: true
});
我目前使用的Web服务允许将一个Base64编码的图像传递给它,然后它将创建图像并使用给定的文件名保存它。但是如果我尝试从phonegap应用程序访问Web服务,它就不起作用了
我曾试图通过ajax访问Web服务,但总是遇到一些错误。是否有更好的方法将图像上载到服务器?由于我无法选择注释作为答案,以下是我的代码,以供将来参考: Phonegap-JS:
navigator.camera.getPicture( function( imgdata ){
var urlimg = "http:// *URL ADDRESS* /mobile.asmx/SaveImage";
var params = new Object();
params.otherinfo = "Whaterver"
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = imgdata.substr(imgdata.lastIndexOf('/')+1);
options.mimeType = "image/jpeg";
options.params = params;
options.chunkedMode = false;
var ft = new FileTransfer();
setTimeout(function() {
ft.upload(imgdata, urlimg, function(){
alert("Success")
}, function(err){
alert("Error: " + JSON.stringify(err));
}, options );
}, 600);
}, function(err){
alert(err);
} , {
quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.CAMERA,
encodingType: navigator.camera.EncodingType.JPEG,
saveToPhotoAlbum: true
});
WebService
[WebMethod]
public void SaveImage()
{
if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("~/devimg"))){
System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/devimg/"));
}
string path = HttpContext.Current.Server.MapPath("~/devimg/").ToString();
var Request = HttpContext.Current.Request;
if (Request.Files.Count > 0){
var file = Request.Files[0];
file.SaveAs( path + file.FileName);
}
}
看。有关设置文件参数和调用Web服务的详细信息。选中此选项可通过添加到“我的Web配置”来修复Http\U状态500。单文件上载功能正常,但在同一url中多个文件上载,如何操作?