Search code examples
jqueryregexjquery-file-upload

Regex to accept specific file types


I am using the BlueImp jQuery File Upload and on its manual, regex code is required to fill the accepted file types you want to upload. I have here the regex so far:

/^image\/(gif|jpe?g|png)$|^application\/(csv|pdf|msword)$|^text\/plain$/i;

But that doesn't support other file types such as docx. What I want are the following file types:

doc,docx,xls,xlsx,ppt,pptx,jpg,png,gif,pdf,txt,csv

Solution

  • The modern MS Office mime types are different from the old-fashioned ones. So when it used to be application/msword, it now is application/vnd.openxmlformats-officedocument.wordprocessingml.document.

    There are quite a few specific mime types now, but they all start with vnd. and then it's either ms- or openxmlformats-.

    So, adding mime types that catch all possible modern Office file types would reflect that:

    /^image\/(gif|jpe?g|png)$|^application\/(csv|pdf|msword|(vnd\.(ms-|openxmlformats-).*))$|^text\/plain$/i;
    //-----------------------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    

    Other mime types are listed here. https://technet.microsoft.com/en-us/library/ee309278%28v=office.12%29.aspx