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 () {
ariaNgFileService.openFileContent({
scope: $scope,
fileFilter: '.torrent',
fileType: 'binary'
}, function (result) {
@ -142,6 +143,7 @@
$scope.openMetalink = function () {
ariaNgFileService.openFileContent({
scope: $scope,
fileFilter: '.meta4,.metalink',
fileType: 'binary'
}, function (result) {

View File

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

View File

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