php dropzone.js 图片上传接受MimeTypes
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17012055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dropzone.js image upload acceptedMimeTypes
提问by user1098178
I am using the dropzone.js plugin to add an image uploader to my application. I know this is probably a really basic question so apologies but what I want to do is limit the file extensions. This works for a single file extension,
我正在使用 dropzone.js 插件向我的应用程序添加图像上传器。我知道这可能是一个非常基本的问题,所以很抱歉,但我想做的是限制文件扩展名。这适用于单个文件扩展名,
<script type="text/javascript">
Dropzone.options.dropzone = {
accept: function(file, done) {
console.log(file);
if (file.type != "image/jpeg") {
done("Error! Files of this type are not accepted");
}
else { done(); }
}
}
</script>
So my question is how to add multiple file extensions, i.e. image/jpeg
, image/png
?
所以我的问题是如何添加多个文件扩展名,即image/jpeg
,image/png
?
Thanks
谢谢
采纳答案by jdepypere
You could add more extensions to your if
, like so:
您可以向您的 中添加更多扩展if
,如下所示:
if (file.type != "image/jpeg" && file.type != "image/png") {
This will check if the file type is different from ALL of the types you specify. For a file to pass the check, it has to be different from image/jpeg AND image/png
这将检查文件类型是否与您指定的所有类型不同。要通过检查的文件,它必须与 image/jpeg AND image/png 不同
Update
更新
I would advise to look at enyo's answersince he is the author of Dropzone.
我建议看看enyo 的回答,因为他是 Dropzone 的作者。
回答by enyo
I'm the author of Dropzone.
我是 Dropzone 的作者。
You should use the acceptedMimeTypes
acceptedFiles
. This behaves exactly the same as the input
element's accept
property. This way, even the fallback will work properly.
您应该使用. 这与元素的属性完全相同。这样,即使回退也能正常工作。acceptedMimeTypes
acceptedFiles
input
accept
Valid acceptedFiles
properties can look like this:
有效acceptedFiles
属性可能如下所示:
audio/*
image/*
image/jpeg,image/png
- etc...
audio/*
image/*
image/jpeg,image/png
- 等等...
EDIT: in the latest versions of Dropzone this property is called acceptedFiles
and it allows you to also define extensions. So this would work:
编辑:在 Dropzone 的最新版本中,这个属性被调用acceptedFiles
,它允许你定义扩展。所以这会起作用:
"audio/*,image/*,.psd,.pdf"
"audio/*,image/*,.psd,.pdf"
(For backwards compatibility acceptedMimeTypes
will still work until the next major release)
(为了向后兼容,acceptedMimeTypes
直到下一个主要版本仍然有效)
回答by Sachin
thanks enyo it worked ....awesome...just paste that line in dropjone.js->
感谢 enyo 它有效....太棒了...只需将该行粘贴到 dropjone.js->
uploadMultiple: true, //upload multiple files
maxFilesize: 1, //1 mb is here the max file upload size constraint
acceptedFiles: ".jpeg,.jpg,.png,.gif",
http://www.dropzonejs.com/#config-acceptedFiles
http://www.dropzonejs.com/#config-acceptedFiles
The default implementation of accept checks the file's mime type or extension against this list. This is a comma separated list of mime types or file extensions. Eg.:
'image/*,application/pdf,.psd'
If the Dropzone is clickable this option will be used as accept parameter on the hidden file input as well.
accept 的默认实现会根据此列表检查文件的 MIME 类型或扩展名。这是一个逗号分隔的 MIME 类型或文件扩展名列表。例如:
'image/*,application/pdf,.psd'
如果 Dropzone 是可点击的,这个选项也将用作隐藏文件输入的接受参数。
回答by user2865151
var myDropzone = new Dropzone('div#profile_pictures',{
acceptedFiles: "image/*", /*is this correct?*/
init: function(){
this.on("success", function(file, data) {
/*..*/
});
}
})
回答by Hafsal Rh
var dz = $("#FileUpload").dropzone({acceptedFiles: ".jpeg"})[0];
回答by rafa_pe
In case someone is interested (I can not comment on Enyo's post): I have had problems with the application of the Dropzone options and after investigating I have noticed that the version of jQuery jquery-3.2.1.min.jsthat I was using was the cause of its malfunction
如果有人感兴趣(我无法对 Enyo 的帖子发表评论):我在应用 Dropzone 选项时遇到了问题,经过调查,我注意到我使用的 jQuery jquery-3.2.1.min.js版本是其故障的原因