0

我有这段代码,用于将表单数据发送到 Web 服务器。工作正常。

$('form').submit(function(){
            //var landmarkID = $(this).parent().attr('data-landmark-id');
            var postData = $(this).serialize();

            // +'&lid='+landmarkID
            $.ajax({
                type: 'POST',
                data: postData,
                url: 'http://cykel.donslund.net/save_bicycle.php',
                success: function(data){
                    console.log(data);
                    alert('Your comment was successfully added');
                },
                error: function(){
                    console.log(data);
                    alert('There was an error adding your comment');
                }
            });
            return false;
        });

在我的应用程序中,我还拍照。如何将该图片与表单数据一起发送到服务器?

4

1 回答 1

0

我昨天也做了同样的。我有照片选择的功能。我给你 Angular 代码和 php 文件。

$scope.selectPicture = function() { // select picture action
  var cameraOptions = {
    quality: 50,
    sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
    destinationType: Camera.DestinationType.FILE_URI,
    targetWidth: 400,
    targetHeight: 400,
    allowEdit: true
  };
  var success = function(data) {
    $scope.$apply(function() {

      $scope.picData = data;
      var fileURL = data;
      var options = new FileUploadOptions();
      options.fileKey = "file";
      options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
      options.mimeType = "image/jpeg";
      options.chunkedMode = true;

      var params = {};
      params.id = numeroData.get();

      options.params = params;


      var ft = new FileTransfer();
      ft.upload(fileURL, encodeURI("http://www.example.net/upload.php?key=" + idData.get()), viewUploadedPictures, function(error) {
        alert('erreur');
      });

      function viewUploadedPictures() {

      }


    });
  };
  var failure = function(message) {
    alert('Failed because: ' + message);
  };

  navigator.camera.getPicture(success, failure, cameraOptions);
};

<?php
header('Access-Control-Allow-Origin: *'); 
if(isset($_GET['key'])){
    move_uploaded_file($_FILES["file"]["tmp_name"], "img/".$_GET['key'].".jpg");
}
?>
于 2015-07-17T08:04:50.487 回答