Ajax phonegap图片发布

时间:2014-01-29 11:47:57

标签: ajax json image cordova

以下是我使用过的代码:

 JSON.stringify({
                request:
                    {

                        "Ticket": "String content",
                        "Picture": {
                           "Name": "blabla",
                            "ImgData": "blabla",

                        },
                    }
            });

我有照片,我用phonegap拍摄,我想用json发布。有可能吗?

2 个答案:

答案 0 :(得分:1)

使用phonegap上传/发布图片

function uploadPhoto(imageURI) {
   var imagefile = imageURI;        
   var ft = new FileTransfer();                     
   var options = new FileUploadOptions();                      
   options.fileKey="vImage1";                      
   options.fileName=imagefile.substr(imagefile.lastIndexOf('/')+1);
   options.mimeType="image/jpeg";  
   var params = new Object();
   params.value1 = "test";
   params.value2 = "param";                       
   options.params = params;
   options.chunkedMode = false;                       
   ft.upload(imagefile, your_service_url, win, fail, options);   
 }

 function win(r) {
   console.log("Code = " + r.responseCode);
   console.log("Response = " + r.response);
   //alert($.parseJSON(r.response))    
 }

function fail(error) {
   console.log("Response = " +  error.code);
 }

答案 1 :(得分:0)

如果你想在json数据中上传它,你需要发送用base64编码的图像,当你拍照时使用destinationType: Camera.DestinationType.DATA_URL