在Blueimp / jQuery-File-Upload中过滤文件

时间:2012-12-17 09:05:54

标签: javascript jquery blueimp

我正在尝试使用Blueimp / jQuery-File-Upload我的项目,我想测试文件扩展名,因为我只想下载照片 有人告诉我在哪里以及如何配置blueimp / jQuery-File-Upload所以我可以这样做

3 个答案:

答案 0 :(得分:0)

如文档中所述,您只需将选项设置为文件类型的正则表达式:

$('#fileupload').fileupload('option', {
            url: '//localhost/',
            maxFileSize: 5000000,
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            process: [
                {
                    action: 'load',
                    fileTypes: /^image\/(gif|jpeg|png)$/,
                    maxFileSize: 20000000 // 20MB
                },
                {
                    action: 'resize',
                    maxWidth: 1440,
                    maxHeight: 900
                },
                {
                    action: 'save'
                }
            ]
        });

答案 1 :(得分:0)

我发现了add method

所以,这是我的.gpx文件代码:



$('#fileupload').fileupload({
  // your fileupload options 
  add:function(e,data){
      data.files.map(function(i){
        if(i.type!='application/gpx+xml'){
          alert('please upload only .gpx files');
        }else{
          data.submit();
        }
    }),
    process: function(e,data){/* your process actions here */}
});




答案 2 :(得分:0)

您可以使用方法add获取过滤器文件:

var $input = $('#upload_input');
$input.fileupload({
  fileInput: $input,
  url: 'YOU_URL',
  // Other options...

  start: function(e, data) {
    // Before start upload... Please wait...
  },
  add: function(e, data) {
    types = /(\.|\/)(gif|jpe?g|png)$/i;
    file = data.files[0]
    if (types.test(file.type) || types.test(file.name)) {
      data.submit();
    } else {
      // alert('Unfortunately, we don’t support that file type. Try again with a PNG, GIF, JPG');
      return;
    }
  },
  done: function(e, data) {
    // After upload... alert('Done');
  },
  fail: function(e, data) {
    // Error. Please try again later...
  }
});