PhoneGap upload Image to server on form submit

前端 未结 4 1620
南旧
南旧 2021-01-31 06:29

I am facing problem here as in phonegap image is uploaded to the server once u select a picture.I don\'t want to upload image before submitting form. Image is uploaded automatic

4条回答
  •  离开以前
    2021-01-31 06:45

    I also faced same problem, but I have done using two server side calls on one click. In this, in first call submit data and get its id in callback using JSON then upload image using this id. On server side updated data and image using this id.

    $('#btn_Submit').on('click',function(event) {
       event.preventDefault();
       if(event.handled !== true)
       {
          var ajax_call = serviceURL; 
          var str = $('#frm_id').serialize();                 
          $.ajax({
          type: "POST",
          url: ajax_call,
          data: str,
          dataType: "json",
          success: function(response){
                  //console.log(JSON.stringify(response))
            $.each(response, function(key, value) { 
                  if(value.Id){                               
                       if($('#vImage').attr('src')){
                             var imagefile = imageURI; 
                              $('#vImage').attr('src', imagefile);
                            /* Image Upload Start */
                              var ft = new FileTransfer();                     
                            var options = new FileUploadOptions();                      
                            options.fileKey="vImage";                      
                            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+'&Id='+Id+'&mode=upload', win, fail, options); 
                          /* Image Upload End */
                       }      
                   }
    
                 }); 
              }
         }).done(function() {
              $.mobile.hidePageLoadingMsg();              
         })
    
       event.handled = true;
      }
      return false;
    });
    

    On server side using PHP

    if($_GET['type'] != "upload"){
      // Add insert logic code
    }else if($_GET['type'] == "upload"){
      // Add  logic for image 
      if(!empty($_FILES['vImage']) ){ 
        // Copy image code and update data  
      }
    }
    

提交回复
热议问题