Skip to content

Commit

Permalink
Fixed #1291 - FileUpload accepts any type of file
Browse files Browse the repository at this point in the history
  • Loading branch information
tugcekucukoglu committed May 21, 2021
1 parent f194dc4 commit b849d14
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/components/fileupload/FileUpload.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declare class FileUpload extends Vue {
auto?: boolean;
maxFileSize?: number;
invalidFileSizeMessage?: string;
invalidFileTypeMessage?: string;
invalidFileLimitMessage?: string;
fileLimit?: number;
withCredentials?: boolean;
Expand Down
39 changes: 34 additions & 5 deletions src/components/fileupload/FileUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export default {
type: String,
default: '{0}: Invalid file size, file size should be smaller than {1}.'
},
invalidFileTypeMessage: {
type: String,
default: '{0}: Invalid file type, allowed file types: {1}.'
},
fileLimit: {
type: Number,
default: null
Expand Down Expand Up @@ -274,20 +278,48 @@ export default {
return !!window['MSInputMethodContext'] && !!document['documentMode'];
},
validate(file) {
if (this.accept && !this.isFileTypeValid(file)) {
this.messages.push(this.invalidFileTypeMessage.replace('{0}', file.name).replace('{1}', this.accept))
return false;
}
if (this.maxFileSize && file.size > this.maxFileSize) {
this.messages.push(this.invalidFileSizeMessage.replace('{0}', file.name).replace('{1}', this.formatSize(this.maxFileSize)));
return false;
}
return true;
},
isFileTypeValid(file) {
let acceptableTypes = this.accept.split(',').map(type => type.trim());
for(let type of acceptableTypes) {
let acceptable = this.isWildcard(type) ? this.getTypeClass(file.type) === this.getTypeClass(type)
: file.type == type || this.getFileExtension(file).toLowerCase() === type.toLowerCase();
if (acceptable) {
return true;
}
}
return false;
},
getTypeClass(fileType) {
return fileType.substring(0, fileType.indexOf('/'));
},
isWildcard(fileType){
return fileType.indexOf('*') !== -1;
},
getFileExtension(file) {
return '.' + file.name.split('.').pop();
},
isImage(file) {
return /^image\//.test(file.type);
},
onDragEnter(event) {
if (!this.disabled) {
event.stopPropagation();
event.preventDefault();
}
},
onDragOver() {
onDragOver(event) {
if (!this.disabled) {
DomHandler.addClass(this.$refs.content, 'p-fileupload-highlight');
event.stopPropagation();
Expand All @@ -299,7 +331,7 @@ export default {
DomHandler.removeClass(this.$refs.content, 'p-fileupload-highlight');
}
},
onDrop() {
onDrop(event) {
if (!this.disabled) {
DomHandler.removeClass(this.$refs.content, 'p-fileupload-highlight');
event.stopPropagation();
Expand Down Expand Up @@ -327,9 +359,6 @@ export default {
this.messages = [];
}
},
isImage(file) {
return /^image\//.test(file.type);
},
clearInputElement() {
this.$refs.fileInput.value = '';
},
Expand Down
6 changes: 6 additions & 0 deletions src/views/fileupload/FileUploadDoc.vue
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ myUploader(event) {
<td>"&#123;0&#125;: Invalid file size, file size should be smaller than &#123;1&#125;."</td>
<td>Message of the invalid fize size.</td>
</tr>
<tr>
<td>invalidFileTypeMessage</td>
<td>string</td>
<td>"&#123;0&#125;: Invalid file type, allowed file types: "&#123;1&#125;.</td>
<td>Message of the invalid file type.</td>
</tr>
<tr>
<td>invalidFileLimitMessage</td>
<td>string</td>
Expand Down

0 comments on commit b849d14

Please sign in to comment.