larryzhao
larryzhao

Reputation: 3213

jQuery-File-Upload content type/extension validation

I am using jQuery-File-Upload with rails 3, it works very well. But I didn't find anything about how to validate the extension or the content type of the upload file on the client-side.

Is there a way to do that?

Of cause I will anyway validate it by Paperclip at server-sid, but I think it would be better to validate it once at client-side.

Upvotes: 2

Views: 3037

Answers (2)

Leo
Leo

Reputation: 2326

The question implicates that you (opposite to the majority) realize that when you detect on just the extention, that you're not checking mime type.

In HTML5 you can use the accept attribute:

<input type="file" accept="video/*" />

You could use multiple (commaseperated?) values. But the specification says space-seperated, but in reality I only see comma-seperated values. Mind: in HTML5 capable browsers adding more validation is redundant.

But in case your question related to other use (like drag/drop file uploads), then you could use javascript in stead of jquery:

if(!(file.type.indexOf('video/') == 0)) {
  alert('nope');
  return false;
  }

Needless to say is that you also should validate on the server side.

Upvotes: 0

Reactormonk
Reactormonk

Reputation: 21690

acceptFileTypes

The regular expression for allowed file types, matches against either file type or file name as only browsers with support for the File API report the file type.

Type: Regular Expression
Example: /(\.|\/)(gif|jpe?g|png)$/i

See https://github.com/blueimp/jQuery-File-Upload/wiki/Options

Upvotes: 2

Related Questions