【问题标题】:Phonegap Filetransfer Upload image to WebservicePhonegap Filetransfer 上传图片到Webservice
【发布时间】:2023-03-07 04:41:02
【问题描述】:

我需要使用 phonegaps Camera and File Transfer API 将图像上传到网络服务。

例如,在Webservice上有:

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

我拥有的当前网络服务允许将 Base 64 编码的图像传递给它,然后它将创建图像并使用给定的文件名保存它。但如果我尝试从 phonegap 应用程序访问网络服务,它就不起作用。

我尝试通过 ajax 访问 web 服务,但一直收到一堆错误。有没有更好的方法将图片上传到服务器?

【问题讨论】:

标签: cordova upload


【解决方案1】:

由于我无法选择评论作为答案,因此我的代码如下所示,以供将来参考: 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
});

网络服务

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

【讨论】:

  • 单文件上传功能正常,但多个文件在同一个url中上传,怎么办?
【解决方案2】:

here。有关设置文件参数和调用 Web 服务的更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 2013-09-20
    • 1970-01-01
    相关资源
    最近更新 更多