将多个图像上传到api - phonegap

时间:2013-12-06 10:52:23

标签: jquery cordova

我在一个phonegap项目中,我有一个页面,其中包含一些要发布到服务器的内容。我已将文本内容发布到服务器,如下所示“

$.ajax({
                                url:"http://xxxx.xxx.xx/mobapp/api/save-data",
                                type:"POST",
                                crossDomain: true,
                                dataType:"json",
                                data: {pliid: document.pliform.pliid.value,idate:document.pliform.date.value,
                                    snum:document.pliform.street_no.value,
                                    sname:document.pliform.street_name.value,state:document.pliform.state.value,
                                    pcode:document.pliform.PostCode.value,
                                    suburb:document.pliform.Suburb.value,
                                            productlist:pl},
                                success: function(data) {
                                     alert("success");
                                     console.log(JSON.stringify(data));

                                },
                                error: function(jqXHR,  textStatus,  errorThrown,data) {
                                     console.log("readyState: " + jqXHR.readyState);
                                }

一切正常,但我想上传一些从设备上拍摄的图片,我对上传多张图片没有任何想法,因为我没有太多将文件上传到api的经验。等你帮忙。感谢。

1 个答案:

答案 0 :(得分:0)

使用phonegap上传图片

 function uploadImage(){
       //Using Camera
       navigator.camera.getPicture(uploadPhoto, onFailcapturePhoto, { quality: 50,destinationType: Camera.DestinationType.FILE_URI }); 
        //Using library            
       navigator.camera.getPicture(uploadPhoto, onFailcapturePhoto, { quality: 50,destinationType: navigator.camera.DestinationType.FILE_URI, sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY});    
 }

  function onFailcapturePhoto(message) {    
     console.log("Message = " + message);
   }

 function uploadPhoto(imageURI) {
    var imagefile = imageURI; 
     $('#vImage').attr('src', imagefile);
/* Image Upload Start */
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);
 }