ng2-file-upload
ng2-file-upload copied to clipboard
allowedFileType allowedMimeType problems
Hi,
I have the following issue trying to make the file type validation works.
If I use only allowedMimeType the validator doesn't work for users without office installed.
I can't make the allowedFileType validator work. How is it expecting the file extension? I make an array like this: ['.png', '.doc']
What am I doing wrong?
It's because you need to specify the MIME media type ex. ['image/png', 'application/pdf', application/msword'] for .png, .pdf, and .doc respectively.
@Bigless27 Thanks for your response.
I know that for the allowedMimeType option I need an array like the one you use in your response but what about for the allowedFileType option?
The allowedMimeType option is not working when the user hasn't Office installed (I think for what I read that the Office package install something to make MimeTypes reconocible) and I can't make allowedFileType option work.
@martinpalmieri Sorry read your question incorrectly!
So after looking at the source code I see that it is doing this
FileType.fileTypeDetection = function (inputFilename) { var types = { 'jpg': 'image', 'jpeg': 'image', 'tif': 'image', 'psd': 'image', 'bmp': 'image', 'png': 'image', 'nef': 'image', 'tiff': 'image', 'cr2': 'image', 'dwg': 'image', 'cdr': 'image', 'ai': 'image', 'indd': 'image', 'pin': 'image', 'cdp': 'image', 'skp': 'image', 'stp': 'image', '3dm': 'image', 'mp3': 'audio', 'wav': 'audio', 'wma': 'audio', 'mod': 'audio', 'm4a': 'audio', 'compress': 'compress', 'rar': 'compress', '7z': 'compress', 'lz': 'compress', 'z01': 'compress', 'pdf': 'pdf', 'xls': 'xls', 'xlsx': 'xls', 'ods': 'xls', 'mp4': 'video', 'avi': 'video', 'wmv': 'video', 'mpg': 'video', 'mts': 'video', 'flv': 'video', '3gp': 'video', 'vob': 'video', 'm4v': 'video', 'mpeg': 'video', 'm2ts': 'video', 'mov': 'video', 'doc': 'doc', 'docx': 'doc', 'eps': 'doc', 'txt': 'doc', 'odt': 'doc', 'rtf': 'doc', 'ppt': 'ppt', 'pptx': 'ppt', 'pps': 'ppt', 'ppsx': 'ppt', 'odp': 'ppt' };
What I think is happening is that allowedFileType can't tell exactly what extension is being uploaded only the general type like image/audio/ect. So what you want to do is allowedFileType: ['image', 'doc'] to allow a png file and a doc file.
@Bigless27 Thanks again!
I will try with what you're telling me. Sounds good!
I'll let you know how it comes out
I tried 'image' and it worked. But when I tried 'compress' and uploaded a file ending with '7z', it didn't work.
i simply added this filter in my config
new FileUploader({
filters: [{name: 'customFileType', fn:(file: FileLikeObject) => {
const ext = file.name.split('.').pop();
return allowedFileType.indexOf(ext) > -1
}}]
});