我无法通过AngularJS从表单接收标题数据

时间:2014-09-19 11:34:16

标签: javascript php angularjs

我正在使用angularJS和插件nervgh / angular-file-upload进行表单  接收标题数据,文本数据和多个文件。多文件运行良好,但问题是我无法接收标题数据和文本数据。这是我的html文件:

<div class="container">
  <div class="row">
    <div class="form-position">
      <div class="thumbnail">
        <div class="form-margin">
          <form class="form-horizontal">
            <div class="form-group">
              <label class="col-lg-3 control-label">Titulo de la entrada</label>
              <div class="col-lg-6">
                <input ng-model="inputTitle" type="text" class="form-control" id="inputTitle" name="inputTitle"
                       placeholder="Introduza el titulo del post" required>
              </div>
            </div>
            <div class="form-group">
              <label class="col-lg-3 control-label">Texto</label>
              <div class="col-lg-6">
                <textarea ng-model="inputText" class="form-control" id="inputText" name="inputText"
                          placeholder="Introduza el texto" rows="5" required></textarea>
              </div>
            </div>
            <div class="form-group">
              <div class="col-lg-10">
                <p ng-class="result" style="padding: 15px;">{{resultMessage}}</p>
                <label class="col-lg-5 control-label">Inserta las Imagenes</label>
                <input type="file" nv-file-select="" uploader="uploader" class="col-lg-7" multiple>
                <table class="table col-lg-offset-1">
                  <thead>
                  <tr>
                    <th width="50%">Name</th>
                    <th ng-show="uploader.isHTML5">Size</th>
                    <th ng-show="uploader.isHTML5">Progress</th>
                    <th>Status</th>
                    <th>Actions</th>
                  </tr>
                  </thead>
                  <tbody>
                  <tr ng-repeat="item in uploader.queue">
                    <td>
                      <strong>{{ item.file.name }}</strong>
                    </td>
                    <td ng-show="uploader.isHTML5" nowrap>{{ item.file.size/1024/1024|number:2 }} MB</td>
                    <td ng-show="uploader.isHTML5">
                      <div class="progress" style="margin-bottom: 0;">
                        <div class="progress-bar" role="progressbar" ng-style="{ 'width': item.progress + '%' }"></div>
                      </div>
                    </td>
                    <td class="text-center">
                      <span ng-show="item.isSuccess"><i class="glyphicon glyphicon-ok"></i></span>
                      <span ng-show="item.isCancel"><i class="glyphicon glyphicon-ban-circle"></i></span>
                      <span ng-show="item.isError"><i class="glyphicon glyphicon-remove"></i></span>
                    </td>
                    <td nowrap>
                      <button type="button" class="btn btn-success btn-xs" ng-click="item.upload()"
                              ng-disabled="item.isReady || item.isUploading || item.isSuccess">
                        <span class="glyphicon glyphicon-upload"></span>
                        Upload
                      </button>
                      <button type="button" class="btn btn-warning btn-xs" ng-click="item.cancel()"
                              ng-disabled="!item.isUploading">
                        <span class="glyphicon glyphicon-ban-circle"></span>
                        Cancel
                      </button>
                      <button type="button" class="btn btn-danger btn-xs" ng-click="item.remove()">
                        <span class="glyphicon glyphicon-trash"></span>
                        Remove
                      </button>
                    </td>
                  </tr>
                  </tbody>
                </table>

              </div>
            </div>
            <div class="form-group">
              <div class="col-lg-offset-2 col-lg-10">
                <button type="submit" class="btn btn-primary" ng-click="uploader.uploadAll()"
                        ng-disabled="submitButtonDisabled">
                  Enviar
                </button>
              </div>
            </div>
          </form>

          <div ng-controller="LoginController">
            <button ng-click="logout()" class="btn-primary">logout</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

这是我的控制器

blog.controller('BlogAdminController',['$scope','FileUploader',function($scope,FileUploader){
  $scope.inputTitle;
  var uploader = $scope.uploader = new FileUploader({
    url: 'entrada.php',
    formData: {'title':$scope.inputTitle}
  });

  //filtros
  uploader.filters.push({
    name: 'imageFilter',
    fn: function(item /*{File|FileLikeObject}*/, options) {
      var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
      return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
    }
  });

  //callbacks
  uploader.onWhenAddingFileFailed = function(item /*{File|FileLikeObject}*/, filter, options) {
    console.info('onWhenAddingFileFailed', item, filter, options);
  };
  uploader.onAfterAddingFile = function(fileItem) {
    console.info('onAfterAddingFile', fileItem);
  };
  uploader.onAfterAddingAll = function(addedFileItems) {
    console.info('onAfterAddingAll', addedFileItems);
  };
  uploader.onBeforeUploadItem = function(item) {
    console.info('onBeforeUploadItem', item);
  };
  uploader.onProgressItem = function(fileItem, progress) {
    console.info('onProgressItem', fileItem, progress);
  };
  uploader.onProgressAll = function(progress) {
    console.info('onProgressAll', progress);
  };
  uploader.onSuccessItem = function(fileItem, response, status, headers) {
    console.info('onSuccessItem', fileItem, response, status, headers);
  };
  uploader.onErrorItem = function(fileItem, response, status, headers) {
    console.info('onErrorItem', fileItem, response, status, headers);
  };
  uploader.onCancelItem = function(fileItem, response, status, headers) {
    console.info('onCancelItem', fileItem, response, status, headers);
  };
  uploader.onCompleteItem = function(fileItem, response, status, headers) {
    console.info('onCompleteItem', fileItem, response, status, headers);
  };
  uploader.onCompleteAll = function() {
    console.info('onCompleteAll');
  };
  console.info('uploader', uploader);
}]);

如果我把它作为例子:

var uploader = $scope.uploader = new FileUploader({
  url: 'entrada.php',
  formData: {'title':"hola"}
});

有效。我收到数据但是我把它放了:

var uploader = $scope.uploader = new FileUploader({
  url: 'entrada.php',
  formData: {'title': $scope.inputTitle}
});

我没有收到标题

的数据
<input ng-model="inputTitle" 
       type="text" 
       class="form-control" 
       id="inputTitle"
       name="inputTitle"
       placeholder="Introduza el titulo del post"
       required>

我有这个php文件,只想在第一次接收数据后检查第一次,我将完成此php文件以保存数据库中的数据:

error_reporting(E_ALL);
ini_set('display_errors', '1');

$images = file_get_contents("php://input");
$objData = json_decode($images);

///compruebo que recibo el fichero primero
if(isset($_FILES['file']) && !empty($_POST)){

        //obtengo grid
        $uploaddir = 'images/blog/';
        $uploadfile = $uploaddir.basename($_FILES['file']['name']);
        if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)){
            //$contenido = array('url'=>$uploadfile);
            //$coleccion->insert($contenido);
            $data = array('success'=>true,'message'=>'El archivo se guardo perfectamente');
            echo json_encode($data);
        }else{
            $data = array('success'=>false,'message'=>'El archivo no se guardo');
            echo json_encode($data);
        }
}else{
    $data = array('success'=>false,'message'=>'No se recivió nada');
    echo json_encode($data);
}

如果这个插件没有解决方案,请给我一个例子,我可以发送带有文本和多个文件的表单。谢谢

1 个答案:

答案 0 :(得分:0)

If you wanna to send POST data with file to server you need to set:

var arr[];
arr['title'] = 'Title'
formData : {arr}