如何自定义 angular 烤面包机消息

how to customize angular toaster message

我正在使用 angular-文件上传。我已经设置了一个批处理文件上传来解析文件名并将它们与存储在数据库中的属性相匹配。文件的结构需要像这样。

01-1998 VRF RD678.pdf

VRF 是管道的名称 RD 是位置的名称 678是一个区号

他们每个人都有自己的 if 语句来检查与数据库中的任何内容匹配的文件。现在,如果某些内容不匹配或命名不正确,则会出现

我想做 3 件事。

  1. 定义一条错误消息,显示文件名和出错的特定 if 语句。如果管道不匹配,我想要文件名和下面的 "no match for pipeline"。

  2. 定义文件名结构不正确时的错误消息。我想要"incorrect filename"下面

  3. 的文件名
  4. 防止功能出错,我希望显示错误信息并允许上传其他文件

我正在尝试使用linq.js,javascript也可以。

这就是我正在尝试做的事情,这个例子是在文件结构不正确的情况下。此错误消息是

TypeError: Cannot read property 'name' of undefined

$scope.upload = function () {
    var files = $scope.files;
    if (files && files.length) {
        for (var i = 0; i < files.length; i++) {
            var file = files[i];
            $scope.pipes.map(function (pip) {
                $scope.pipeLookup[pip['PipeAb']] = pip;
            });
            $scope.locations.map(function (loc) {
                $scope.locationLookup[loc['LocationAb']] = loc;
            });
            $scope.locationCodes.map(function (locCode) {
                $scope.locationCodeLookup[locCode['LocationCodeAb']] = locCode;
            });
            var matchesPip = file.name.match(/^\d+\D\d+\s*(\S*\s*)(\S*)/i);
            var matchesLoc = file.name.match(/^\d+\D\d+\s*?(\S*)\s*(\S*?)(\d+)\./i);
            var matchesLocCode = file.name.match(/^(\d+\D\d+)\s*?(\S*)\s*(\S*?)(\d+)\./i);
            $scope.pip = $scope.pipeLookup[matchesPip[1]];
            $scope.loc = $scope.locationLookup[matchesLoc[2]];
            $scope.locCode = $scope.locationCodeLookup[matchesLocCode[4]];
            if ($scope.pip == null) {

                $scope.pip = Enumerable.From(files)
                            .Where("x => x.files.name != '" + matchesPip[0] + "'").ToArray();

                toaster.pop('error', matchesPip[0]);
                console.log(matchesPip[0])
            }
            if ($scope.loc == null) {
                toaster.pop('error', matchesLoc[0]);
                console.log(matchesLoc[0])
            }
            if ($scope.locCode == null) {
                toaster.pop('error', matchesLocCode[0]);
                console.log(matchesLocCode[0])
            }
            $upload.upload({
                url: '/api/apiBatchPipeLine',
                fields: {
                    'typeId': 1,
                    'companyId': $scope.companyId.CompanyId,
                    'companyName': $scope.companyId.CompanyName,
                    'documentDate': $scope.model.documentDate,
                    'pipeId': $scope.pip['PipeId'],
                    'pipeName': $scope.pip['PipeName'],
                    'locationId': $scope.loc['LocationId'],
                    'locationAb': $scope.loc['LocationAb'],
                    'locationCodeId': $scope.locCode['LocationCodeId'],
                    'locationCodeAb': $scope.locCode['LocationCodeAb']
                },
                file: file
            }).progress(function (evt) {
                var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
            }).success(function (data, status, headers, config) {
                toaster.pop('success', config.file.name);
                console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
            }).error(function (err, result, config) {
                toaster.pop('error', config.file.name);
                console.log(err, result);
            });

        }
    }
};

ngFileUpload error 事件回调接收 4 个参数,如:

https://github.com/danialfarid/ng-file-upload/blob/master/dist/ng-file-upload-all.js#L509-514

promise.error = function (fn) {
   promise.then(null, function (response) {
       fn(response.data, response.status, response.headers, config);
   });
   return promise;
};

headers是第三个参数,config是第四个参数。在您的代码中 config 引用 headersheaders.file 未定义,所以这就是您得到错误 TypeError: Cannot read property 'name' of undefined.

的原因

变化:

.error(function (err, result, config) {
   ...
})

收件人:

.error(function (err, result, headers, config) {
   ...
})