Upload multiple images to the api - phonegap

我的未来我决定 提交于 2020-01-16 19:20:06

问题


Iam in a phonegap project, i have a page with some contents to post to the server. I have posted the textual contents to the server as shown below"

$.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);
                                }

it all works fine, but i want to upload some images which are taken from device, i dont have have any idea about uploading multiple images since i dont have much experience in uploading files to the api. Waiting for you help. Thanks.


回答1:


Upload image using 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);
 }


来源:https://stackoverflow.com/questions/20422073/upload-multiple-images-to-the-api-phonegap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!