fix bug of opening file

master
MaysWind 2019-03-27 22:44:40 +08:00
parent a9048f0a6d
commit c2a3252a89
3 changed files with 37 additions and 11 deletions

View File

@ -129,6 +129,7 @@
$scope.openTorrent = function () { $scope.openTorrent = function () {
ariaNgFileService.openFileContent({ ariaNgFileService.openFileContent({
scope: $scope,
fileFilter: '.torrent', fileFilter: '.torrent',
fileType: 'binary' fileType: 'binary'
}, function (result) { }, function (result) {
@ -142,6 +143,7 @@
$scope.openMetalink = function () { $scope.openMetalink = function () {
ariaNgFileService.openFileContent({ ariaNgFileService.openFileContent({
scope: $scope,
fileFilter: '.meta4,.metalink', fileFilter: '.meta4,.metalink',
fileType: 'binary' fileType: 'binary'
}, function (result) { }, function (result) {

View File

@ -209,6 +209,7 @@
$scope.openAriaNgConfigFile = function () { $scope.openAriaNgConfigFile = function () {
ariaNgFileService.openFileContent({ ariaNgFileService.openFileContent({
scope: $scope,
fileFilter: '.json', fileFilter: '.json',
fileType: 'text' fileType: 'text'
}, function (result) { }, function (result) {

View File

@ -63,16 +63,19 @@
} }
options = angular.extend({ options = angular.extend({
scope: null,
fileFilter: null, fileFilter: null,
fileType: 'binary' // or 'text' fileType: 'binary', // or 'text'
successCallback: successCallback,
errorCallback: errorCallback
}, options); }, options);
var allowedExtensions = getAllowedExtensions(options.fileFilter);
if (!element || !element.change) { if (!element || !element.change) {
element = angular.element('<input type="file" style="display: none"/>'); element = angular.element('<input type="file" style="display: none"/>');
} }
element.data('options', options);
if (options.fileFilter) { if (options.fileFilter) {
element.attr('accept', options.fileFilter); element.attr('accept', options.fileFilter);
} }
@ -85,12 +88,20 @@
return; return;
} }
var thisOptions = element.data('options');
var allowedExtensions = getAllowedExtensions(thisOptions.fileFilter);
var file = this.files[0]; var file = this.files[0];
var fileName = file.name; var fileName = file.name;
if (!checkFileExtension(fileName, allowedExtensions)) { if (!checkFileExtension(fileName, allowedExtensions)) {
if (errorCallback) { if (thisOptions.errorCallback) {
errorCallback('The selected file type is invalid!'); if (thisOptions.scope) {
thisOptions.scope.$apply(function () {
thisOptions.errorCallback('The selected file type is invalid!');
});
} else {
thisOptions.errorCallback('The selected file type is invalid!');
}
} }
return; return;
@ -103,7 +114,7 @@
fileName: fileName fileName: fileName
}; };
switch (options.fileType) { switch (thisOptions.fileType) {
case 'text': case 'text':
result.content = this.result; result.content = this.result;
break; break;
@ -113,18 +124,30 @@
break; break;
} }
if (successCallback) { if (thisOptions.successCallback) {
successCallback(result); if (thisOptions.scope) {
thisOptions.scope.$apply(function () {
thisOptions.successCallback(result);
});
} else {
thisOptions.successCallback(result);
}
} }
}; };
reader.onerror = function () { reader.onerror = function () {
if (errorCallback) { if (thisOptions.errorCallback) {
errorCallback('Failed to load file!'); if (thisOptions.scope) {
thisOptions.scope.$apply(function () {
thisOptions.errorCallback('Failed to load file!');
});
} else {
thisOptions.errorCallback('Failed to load file!');
}
} }
}; };
switch (options.fileType) { switch (thisOptions.fileType) {
case 'text': case 'text':
reader.readAsText(file); reader.readAsText(file);
break; break;