angularjs - ng-file-upload not binding model to dynamic created HTML form

时间:2015-07-29 09:29:18

标签: javascript html angularjs ng-file-upload

我想使用ng-repeat从动态生成的HTML表单上传图像文件。 我正在使用ng-file-upload模块上传单个图像文件(https://github.com/danialfarid/ng-file-upload)。 当我从静态HTML上传文件时,它正常工作。 但是当我尝试从动态生成的HTML上传文件时,它不会按预期工作。该文件没有上传,也在firefox控制台中给出错误,如下所示:

Error: Argument 2 of FormData.append is not an object.

如果我们将文件控件的ng-model分配为null,则表单已成功提交。 例如;如果

<input name='img' type='file' value='' ng-model='data.imageFile' 'ngf-select' accept='image/*' />

$scope.data.imageFile = null;

然后其他参数将由HTTP服务提交并正常存储到数据库,但文件将不会上传。

在这种动态生成HTML的情况下,有没有办法将文件对象分配给输入[type = file]?

此处创建代码 PLUNKER

http://plnkr.co/edit/S7hnVJnuDjWUMQ6PYewk?p=preview

1 个答案:

答案 0 :(得分:2)

是的,有一种方法可以将input type = file分配给动态生成的html。不仅在页面加载时动态生成,而且在通过angular添加新输入type = file时也是如此。我刚刚这样做了,它有效!我非常兴奋,我在这里发布所有技巧。所有我要求的回报都是请在您的解决方案中使用它时投票。问题和答案现在都是0点,但我可以证明这是一个有效的解决方案。

     <input type="file" class="form form-control" placeholder="Section Image" file-model2="fileUploadFile2[getImageIndex(c.ChapterNbr, $index)][$index]" />

请注意,有二维数组和此input = file在ng-repeat中的ng-repeat内,当用户按下+ Add按钮时动态添加。

角度侧的

,在getImageIndex:

        var chIndex = 0;
        var sIndex = 0;

        $scope.getImageIndex = function (chNbr, sectionNbr) {
            for (var i = 0; i < $scope.chapters.length; i++) {
                if ($scope.chapters[i].ChapterNbr == chNbr) {
                    chIndex = i;
                    sIndex = sectionNbr;
                    return i;
                };
            };
        };

这纯粹是为了获取索引(第一维和第二维,特定于我的设置)。 我使用在StackOverflow中某处发布的指令,我非常感谢,实际获取文件字节和信息,它是这样的:

       .directive('fileModel2', ['$parse', function ($parse) {
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                element.bind('change', function (e) {
                    $parse(attrs.fileModel2)
                    .assign(scope, element[0].files[0]);
                    scope.$apply();
                    scope.getFile2(scope.$eval(attrs.indexNumber));
                });
            }
        };
    }])
    .factory('fileReaderFactory', function ($q, $log) {
        return {
            onLoad: function (reader, deferred, scope) {
                return function () {
                    scope.$apply(function () {
                        deferred.resolve(reader.result);
                    });
                };
            },
            onError: function (reader, deferred, scope) {
                return function () {
                    scope.$apply(function () {
                        deferred.reject(reader.result);
                    });
                };
            },
            onProgress: function (reader, scope) {
                return function (event) {
                    scope.$broadcast("fileProgress",
                        {
                            total: event.total,
                            loaded: event.loaded
                        });
                };
            },
            getReader: function (deferred, scope) {
                var reader = new FileReader();
                reader.onload = this.onLoad(reader, deferred, scope);
                reader.onerror = this.onError(reader, deferred, scope);
                reader.onprogress = this.onProgress(reader, scope);
                return reader;
            },

            readAsDataURL: function (file, scope) {
                var deferred = $q.defer();

                var reader = this.getReader(deferred, scope);
                reader.readAsDataURL(file);

                return deferred.promise;
            }
        }
    }
    );

该指令触发getFile2,它执行Filereader以在预览图像之前读取字节。 最后,预览图像:

         $scope.getFile2 = function () {
            console.log($scope.fileUploadFile2[chIndex][sIndex]);
            if ($scope.fileUploadFile2[chIndex][sIndex]) {
                fileReaderFactory.readAsDataURL($scope.fileUploadFile2[chIndex][sIndex], $scope)
                    .then(function (result) {
                        $scope.chapters[chIndex].Sections[sIndex].sectionImgPreview = result;
                    });
            }
        };

这是用于预览图像的html:

     <img ng-if="s.sectionImgPreview" class="img-responsive" ng-src="{{s.sectionImgPreview}}" alt="" onerror="this.src='@Url.Content("~/Content/Images/ToyApp.png")';" />

此时,$ scope.fileUploadFile2 [chIndex] [sIndex]已准备好发布到后端,在我的情况下,后端是一个C#Controller,它接受包含课程章节和部分的整个JSON,图像二进制文件和视频,文本和HTML,进入一个复杂的类,然后将信息存储到数据库模式中。