首页 > 解决方案 > Plupload - 按 mime 类型限制文件类型

问题描述

尝试使用 plupload 将上传限制为某些文件类型。仅具有扩展名的过滤器是不够的,因为可以重命名文件扩展名。

例如,我们目前正在过滤以仅允许扩展名为“jpg,jpeg,gif,png”的图像文件。我遇到了一个问题,用户尝试上传file.jpg 但 mime 类型为“ image/webp ”。

我们还想强制使用有效的 mime 类型。

我们目前的代码是这样的:

// Initialize the widget when the DOM is ready
$(function() {
    $("#uploader").plupload({

        // General settings
        runtimes : 'html5,html4',
        url : "index.cfm?fa=Media.AddMedia",

        // Maximum file sizes
        max_file_size : '1mb',

        chunk_size: '1mb',

        // Specify what files to browse for
        filters : [
            {title : "Image files", extensions : "jpg,jpeg,gif,png"}
        ],

        // Rename files by clicking on their titles
        rename: true,

        // Sort files
        sortable: true,

        // Enable ability to drag'n'drop files onto the widget (currently only HTML5 supports that)
        dragdrop: true,

        // Views to activate
        views: {
            list: true,
            thumbs: true, // Show thumbs
            active: 'thumbs'
        }
    });

    $("#uploader").on("complete", function() {
        window.parent.closePopWin();
    });
});

我尝试按照https://www.plupload.com/docs/v2/Options#filters.mime_types中的内容添加 mime_types 过滤器,但似乎没有任何东西阻止无效的 mime 类型。

可以这样做还是我必须在服务器端进行验证?

重要提示:我们必须使用 Firefox 运行此网站!

太感谢了!拍

标签: jqueryplupload

解决方案


推荐阅读