NewUser
NewUser

Reputation: 13333

jQuery file upload allow only doc files

I am using the JQuery File Upload plugin.

I want to upload only files with the extensions like .doc, .docx, .pdf. So can someone tell me how to allow the plugin only to upload .doc, .docx, .pdf files. Any help and suggestions will be really appreciable. Thanks

Upvotes: 3

Views: 9551

Answers (6)

Gurpreet Singh
Gurpreet Singh

Reputation: 383

Here is two way to do this:

First:

$('#file').on( 'change', function() {
   myfile= $( this ).val();
   var ext = myfile.split('.').pop();
   if(ext=="docx" || ext=="doc"){
       alert(ext);
   } else{
       alert(ext);
   }
});

In alert box showing you actual file type alert(ext);

Second:

The extensions should start with a dot "."

Upvotes: 0

Pedro Henrique
Pedro Henrique

Reputation: 146

search the line in the file: server/php/UploadHandler.php

'accept_file_types' => '/\.(gif|jpe?g|png)$/i'

Add to the end, thus:

'accept_file_types' => '/\.(gif|jpe?g|png|pdf)$/i'

Upvotes: 0

Giannis Grivas
Giannis Grivas

Reputation: 3412

There is a parameter at options of the plugin for this purpose.

 options: {
   acceptFileTypes: 'File type not allowed',
}

The explanation is here : fileupload-validate.js

Upvotes: 0

jcruz
jcruz

Reputation: 718

Have you tried setting the accept attribute on the input tag

<input id="fileupload" type="file" name="file" accept=".doc,.docx,.pdf">

Upvotes: 0

ashokhein
ashokhein

Reputation: 1058

var ext = $('#my_file_field').val().split('.').pop().toLowerCase();
if($.inArray(ext, ['doc','docx','pdf']) == -1) {
    alert('invalid extension!');
}

Upvotes: 0

Rohan Kumar
Rohan Kumar

Reputation: 40639

You can try to use acceptFileTypes option like,

var acceptFileTypes =/^application\/(pdf|msword)$|^doc$|^docx$/i;

Read more about options

Upvotes: 3

Related Questions