Angularjs文件上传不起作用

时间:2014-08-21 03:29:56

标签: angularjs file-upload laravel laravel-4 angularjs-directive

您好我正在尝试使用danialfarid库上传文件。

https://github.com/danialfarid/angular-file-upload

但是,当我询问服务器是否收到文件时,我会一直回复。

以下是我的控制器中的上传功能:

  $scope.onFileSelect = function($files) {
    //$files: an array of files selected, each file has name, size, and type.
    for (var i = 0; i < $files.length; i++) {

      $scope.upload = $upload.upload({
        url: 'menu/update', //upload.php script, node.js route, or servlet url
        method: 'PUT',
        //headers: {'header-key': 'header-value'},
        //withCredentials: true,
        data: {myObj: $scope.myModelObj},
        file: $files[i], 


      }).progress(function(evt) {
        console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
      }).success(function(data, status, headers, config) {
        // file is uploaded successfully
        console.log(data);
      });
      //.error(...)
      //.then(success, error, progress); 
      // access or attach event listeners to the underlying XMLHttpRequest.
      //.xhr(function(xhr){xhr.upload.addEventListener(...)})
    }
    /* alternative way of uploading, send the file binary with the file's content-type.
       Could be used to upload files to CouchDB, imgur, etc... html5 FileReader is needed. 
       It could also be used to monitor the progress of a normal http post/put request with large data*/
    // $scope.upload = $upload.http({...})  see 88#issuecomment-31366487 for sample code.
  };

以下是来自Chrome控制台的请求有效负载我发送:

--WebKitFormBoundaryozbcZE1o6Nnkd2F7
Content-Disposition: form-data; name="myObj"

undefined
------WebKitFormBoundaryozbcZE1o6Nnkd2F7
Content-Disposition: form-data; name="file"; filename="avatar-mini4.jpg"
Content-Type: image/jpeg


------WebKitFormBoundaryozbcZE1o6Nnkd2F7--

laravel中的服务器代码

public function upload(){
        Input::hasFile('file')
            return Response::json(array(    $file), 200
                );  
}

0 个答案:

没有答案