Phonegap Filetransfer将图像上传到Webservice [英] Phonegap Filetransfer Upload image to Webservice

查看:123
本文介绍了Phonegap Filetransfer将图像上传到Webservice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用phonegaps相机和文件传输API将图像上传到网络服务。

I need to upload an image to a webservice using phonegaps Camera and File Transfer API.

例如,在Webservice上有:

So for example, on the Webservice have :

[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应用程序访问webservice。

The current webservice i have allows for a Base 64 encoded image to be passed into it which will then create the image and save it using the given file name. But it doesnt work if i try to access the webservice from the phonegap app.

我试图通过ajax访问webservice,但只是不断得到一堆错误。是否有更好的方法将图片上传到服务器?

Ive tried to access the webservice through ajax but just kept getting a bunch of error. is there a better way to upload the image to the server?

推荐答案

由于我无法选择评论作为答案,我的代码看起来像以备将来参考:
Phonegap JS:

As i cannot choose a comment as an answer, here is what my code looks like for future reference: 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);
 }
}

这篇关于Phonegap Filetransfer将图像上传到Webservice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆