首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与其他数据一起上传的文件

与其他数据一起上传的文件
EN

Stack Overflow用户
提问于 2016-05-05 05:05:27
回答 2查看 1.2K关注 0票数 0

我想用angularjs上传文件和其他数据。我是FormData,但是我从服务器端接收空白数组。

这是我的表格

代码语言:javascript
复制
<form ng-submit="beatSubmit()" name="beatform" enctype="multipart/form-data">
  <input type="text" id="beat-name" ng-model="beatData.title" required="required" />
  <input type="file" id="image" file-model="image" />
  <input type="file" id="tagged_file" file-model="tagged_file" accept="audio/mp3" />
  <input type="file" id="untagged-beat" file-model="untagged_file" accept="audio/mp3" />
  <input type="text" class="form-control" id="price1" ng-model="beatData.price1">
</form>

这里是我的控制器和FileModel指令

代码语言:javascript
复制
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]);
                });
            });
        }
    };
  }]);
 // This is controller part in another file
   $scope.beatSubmit = function(){

            var image = $scope.image;
            var tagged_file = $scope.tagged_file;
            var untagged_file = $scope.untagged_file;

            var response = BEAT.uploadBeat($scope.beatData,image,tagged_file,untagged_file);
            response.success(function(response){
                console.log(response);
            });
        }

,这是我的服务

代码语言:javascript
复制
uploadBeat:function(data,image,tagged_file,untagged_file){
            var fd = new FormData();
            fd.append('image', image);
            fd.append('tagged_file', tagged_file);
            fd.append('untagged_file', untagged_file);

            angular.forEach(data, function(value, key) {
                fd.append(key,value);
            });
            console.log(fd); // fd is null , I don't know why?
            var req = {
                         method: 'POST',
                         transformRequest: angular.identity,
                         url: 'api/upload_music',
                         data: fd,
                         headers:{
                             'Content-Type': undefined,
                             }
                        }
            return $http(req);

        }

当我尝试从服务器端获取这些数据时,它将返回null。我花了更多的时间来解决这个问题,但我没有得到任何解决办法。如果有人知道请帮帮我。提前谢谢。

EN

回答 2

Stack Overflow用户

发布于 2016-05-05 05:28:03

将此标题详细信息添加到$http中

代码语言:javascript
复制
$http({
                    method: 'POST',
                    url: '',
                    headers: {
                              'X-Requested-With': 'XMLHttpRequest',
                              'Accept':'*/*',
                              'Content-type':'application/x-www-form-urlencoded;charset=utf-8'
                             },
                    params:data,
                    timeout:4000
                  });

Laravel不允许在没有相同域策略的情况下提交ajax请求

票数 0
EN

Stack Overflow用户

发布于 2016-05-05 07:03:49

我发现了一个错误,我必须从表单中删除enctype=“多部分/表单-数据”。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37042775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档