javascript 使用 phonegap (w/jquery ajax) 将图像上传到服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9684458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:26:31  来源:igfitidea点击:

Uploading an image to server with phonegap (w/jquery ajax)

javascriptjquerycordova

提问by ChrisSherwood

I am trying to put together a small phonegap app to upload images to a collaboration site. I have looked over tutorial and API. So far I have the image captured but I am stuck on getting it to the server. I was hoping to use jquery for ajax it over. My jquery seems to stop the image capture btn working, when I take it out the btn works and captures an image. Is there another way other than jquery I can do this or I am just being a plonker and missing something? I am very new to phonegap.

我正在尝试组合一个小的 phonegap 应用程序来将图像上传到协作站点。我查看了教程和 API。到目前为止,我已经捕获了图像,但我一直坚持将其发送到服务器。我希望将 jquery 用于 ajax。我的 jquery 似乎停止图像捕获 btn 工作,当我取出 btn 工作并捕获图像时。除了jquery,还有其他方法可以做到这一点,还是我只是一个笨蛋而错过了什么?我对phonegap很陌生。

$(document).ready(function() {

function capturePhoto() {
  // Take picture using device camera and retrieve image as base64-encoded string
  navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50 });

}


function onFail(message) {
  alert('Failed because: ' + message);
}

 function onPhotoDataSuccess(imageData) {

  var url = 'http://www.creativetree.co/phonegapupload.php';
  var params = {image: imageData};
  navigator.notification.alert('photo taken');    


  $.post(url, params, function(data) {

            // Display the selected image on send complete
            $('#image').attr('src', 'data:image/jpeg;base64,' + params['image']);

        });
}

});

回答by gmh04

The phonegap api has an example of this: http://docs.phonegap.com/en/1.5.0/phonegap_file_file.md.html#FileTransfer

phonegap api 有一个这样的例子:http://docs.phonegap.com/en/1.5.0/phonegap_file_file.md.html#FileTransfer

回答by BigBalli

posting an image as base64 via ajax is fine, I have done it in apps several times. However, the tricky part may be encoding/decoding and making sure the php processes it correctly. Make sure the PHP doesn't expect an actual image (binary).

通过 ajax 将图像发布为 base64 很好,我已经在应用程序中做过几次了。然而,棘手的部分可能是编码/解码并确保 php 正确处理它。确保 PHP 不期望实际图像(二进制)。