jQuery-File-Upload content type/extension validation

人盡茶涼 提交于 2019-12-11 03:25:51

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/9110032/jquery-file-upload-content-type-extension-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!