Phonegap Filetransfer将图像上传到Webservice
问题描述:
我需要使用phonegaps Camera和File Transfer API将图像上传到webservice。Phonegap Filetransfer将图像上传到Webservice
因此,例如,在web服务有:
[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服务我有允许Base 64编码的图像被传递到它然后它将创建图像并使用给定的文件名保存它。但它不工作,如果我尝试从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);
}
}
+0
单个文件上传功能工作正常,但多个文件上传在同一个网址中,该怎么办? – Sathiyaraj
检查此问题http://stackoverflow.com/questions/15502344/upload-the-image-to-the-remote-server-with-phonegap-ajax-and-web-service-net/15503354#15503354 –
托管通过向我的Web配置添加 \t \t 来修复Http_Status 500。 –
Sidedcore