如何重置Angular JS中的文件上传控件

时间:2016-06-03 16:58:29

标签: javascript angularjs file-upload

在我的应用中,我有一个文件上传控件,

   <label>Upload File</label>
    <div class="ui-select">
            <input type="file" name="files" ng-files="getTheFiles($files)" />
    </div>

有助于上传文件的控制器代码是......

    $scope.file = null;

    //UPLOAD FILE CODE
    var formdata;
    $scope.getTheFiles = function ($files) {
        console.log($files[0].type);
        formdata = new FormData();
        angular.forEach($files, function (value, key) {
            formdata.append(key, value);
        });
    };

    // NOW UPLOAD THE FILES.
    $scope.uploadFiles = function () {
        var request = {
            method: 'POST',
            url: BasePath + 'uploadNative/UploadFiles/',
            data: formdata,
            headers: {
                'Content-Type': undefined
            }
        };

        // SEND THE FILES.
        console.log(formdata);

        if (formdata != null || formdata != undefined) {
            $http(request)
                .success(function (response) {
                    if (response != "Failed!" && response !="FileAlreadyUploaded") {
                        console.log("Succeeds");
                    }
                    else if(response == "FileAlreadyUploaded")
                    {
                        angular.element("#fileUploadModal").modal();

                    }
                    else {
                        console.log("Failed");
                    }
                })
                .error(function () {
                });
        }

我希望在成功或失败时清除文件上传控件,即

之后

angular.element("#fileUploadModal").modal();

显示模态,我希望清除控件。我该怎么办?请解释一下。

2 个答案:

答案 0 :(得分:2)

id设为file upload control

HTML:

<input id= "control" type="file" name="files" ng-files="getTheFiles($files)" />

控制器:

angular.element("#control").value = "";

答案 1 :(得分:1)

添加 - 点击您的输入:

<input ng-click="reset()" id= "control" type="file" name="files" ng-files="getTheFiles($files)" />

并在控制器中创建重置方法:

 $scope.reset= function () {
    angular.element("input[type='file']").val(null);
};