使用AngularJS和symfony2上传文件

时间:2015-03-29 05:27:13

标签: angularjs symfony

我使用http://jsfiddle.net/JeJenny/ZG9re/中提供的AngularJS示例从本地计算机上传文件。 我使用angularJS(前端)和symfony2(后端),我必须发送电子邮件附件(作为休息网络服务),但我有一个问题,我没有得到任何电子邮件附件,这是我得到的错误在控制台中:

enter image description here

这是我的代码:

<div ng-controller="MailNewCtrl1">
<input type="file" nv-file-select="" id="id_file"  uploader="uploader" file-model="myFile" >
<button id="id_submit" class="btn btn-info w-xs" ng-click='usersend();'>send</button></div>

这是控制器:

'use strict';

angular.module('app', [
    'ngAnimate',
    'ngCookies',
    'ngResource'
])
.controller('MailNewCtrl1', ['$scope', '$http' ,'fileUpload' , function($scope,$http,fileUpload) {
 $scope.errors = [];
      $scope.msgs = [];
      $scope.usersend = function() {
      $scope.errors.splice(0, $scope.errors.length); // remove all error messages
      $scope.msgs.splice(0, $scope.msgs.length);
       var file = $scope.myFile;
        console.log('file is ' + JSON.stringify(file));
        var uploadUrl = "/fileUpload";
        fileUpload.uploadFileToUrl(file, uploadUrl);

    $http({method: 'POST', url: 'theURLsendMail?myFile='+file}).success(function(data, status, headers, config){ 
     if (data.msg != '')
                        {
                            $scope.msgs.push(data.msg);
                            alert("sucess send");
                        }
                        else
                        {
                            $scope.errors.push(data.error);
                        }
                    }).error(function(data, status) { // called asynchronously if an error occurs
// or server returns response with an error status.
                        $scope.errors.push(status);
                    });

        }
    }]);

app.directive('fileModel', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            var model = $parse(attrs.fileModel);
            var modelSetter = model.assign;

            element.bind('change', function(){
                scope.$apply(function(){
                    modelSetter(scope, element[0].files[0]);
                });
            });
        }
    };
}]);

app.service('fileUpload', ['$http', function ($http) {
    this.uploadFileToUrl = function(file, uploadUrl){
        var fd = new FormData();
        fd.append('file', file);
        $http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        })
        .success(function(){
        })
        .error(function(){
        });
    }
}]);

我甚至对要存储的文件夹的权利进行了解决,它包含了所有权限

感谢您的帮助

0 个答案:

没有答案