我使用Angular-Upload提交文件。我需要提取文件名的2个部分,以用作返回api控制器的formData。这些文件目前名为mm / yyyy PipeName PipelineLocation.pdf 我需要实际删除日期,然后将PipeName添加到' pipeName'''''然后将PipelineLocation添加到' locationName':'& #39; 我使用的示例文件名为' 02-2011 P3D4LB38A2 DDEC33D.pdf'和' 11-2008 ED34PL89G5 23FFWC580.pdf'
我可以看到具有name属性的数组,但我不知道如何访问它。
Plunker
app.controller('MainCtrl', function($scope, $upload) {
$scope.$watch('files', function () {
$scope.upload($scope.files);
});
$scope.result={};
$scope.upload = function (files) {
if (files && files.length) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
$upload.upload({
url: '/api/apiBatchPipeLine',
fields: {
'typeId': 1,
'companyId': $scope.companyId.CompanyId,
'documentDate': $scope.model.documentDate,
'companyName': $scope.CompanyName,
'pipeName': ,
'locationName': ,
'typeName': 'Pipeline Reports'
},
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) {
console.log('file ' + config.file.name + 'uploaded. Response: ' +
JSON.stringify(data));
});
}
}
};
});
UPDATE 错误
TypeError: undefined is not a function
at l.$scope.upload (https://localhost:44300/MyScripts/Controllers/BatchSubmit/batchSubmitPipesController.js:68:31)
at Object.fn (https://localhost:44300/MyScripts/Controllers/BatchSubmit/batchSubmitPipesController.js:55:16)
at l.$get.l.$digest (https://localhost:44300/Scripts/angular.min.js:123:445)
at l.$get.l.$apply (https://localhost:44300/Scripts/angular.min.js:126:362)
at bg.$$debounceViewValueCommit (https://localhost:44300/Scripts/angular.min.js:219:34)
at bg.$setViewValue (https://localhost:44300/Scripts/angular.min.js:218:263)
at https://localhost:44300/Scripts/angular-file-upload-all.min.js:2:1349
at https://localhost:44300/Scripts/angular.min.js:138:513
at e (https://localhost:44300/Scripts/angular.min.js:40:339)
at https://localhost:44300/Scripts/angular.min.js:44:375
第68行
if (file.name.test(regex)) { // used to validate the filename
第55行
$scope.$watch('files', function (files) { // this can be simplified like so.
$scope.upload(files);
更新PICS
答案 0 :(得分:1)
您可以使用正则表达式提取文件名的所需部分:
实现此目的的正则表达式可能类似于:
^\d+\D\d+\s*(\S*)\s*(\S*)\..*$
这是寻找任何数字序列,后跟非数字,后跟任何数字序列。然后是所有空格,然后是一系列非空格(你的pipename),然后是空格和另一个非空格序列(你的位置名)。最后的最后一点是确保它与文件扩展名匹配。
app.controller('MainCtrl', function($scope, $upload) {
var regex = /^\d+\D\d+\s*?(\S*)\s*(\S*)\..*$/i;
$scope.$watch('files', function (files) { // this can be simplified like so.
$scope.upload(files);
});
$scope.result={};
$scope.upload = function (files) {
if (files && files.length) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
if(regex.test(file.name)) { // used to validate the filename
var matches = file.name.match(regex);
$upload.upload({
url: '/api/apiBatchPipeLine',
fields: {
'typeId': 1,
'companyId': $scope.companyId.CompanyId,
'documentDate': $scope.model.documentDate,
'companyName': $scope.CompanyName,
'pipeName': matches[1],
'locationName': matches[2],
'typeName': 'Pipeline Reports'
},
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) {
console.log('file ' + config.file.name + 'uploaded. Response: ' +
JSON.stringify(data));
});
}
}
}
};
});