在上传jQuery文件上传之前验证文件并启用上传按钮

时间:2015-10-19 08:06:46

标签: javascript jquery file-upload jquery-file-upload

我使用最新版本(9.11.2)上传文件。我已经实现了一个按钮,供用户单击以使用add选项开始上传。现在我还想添加文件验证以确保文件是图像。 I found a solution to to this。这对我来说非常合适,但前提是我删除了下面的add选项。

$('#fileupload').fileupload({
     forceIframeTransport: true,
     url: nexus.admintoolapi + 'user/picture',
     // The regular expression for allowed file types, matches
     // against either file type or file name:
     acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
     add: function (e, data) {
         $uploadButton.show();
         $uploadButton.on('click', function () {
            data.submit();
         });
     },
     success: function (e, data) {
     },
     fail: function (e, data) {
     },
     done: function (e, data) {
     }
     }).on('fileuploadprocessalways', function (e, data) {
        var currentFile = data.files[data.index];
        if (data.files.error && currentFile.error) {
          // there was an error, do something about it
          console.log(currentFile.error);
        }
    });

因此上述代码根本不会运行.on('fileuploadprocessalways'。但这会:

$('#fileupload').fileupload({
     forceIframeTransport: true,
     url: nexus.admintoolapi + 'user/picture',
     // The regular expression for allowed file types, matches
     // against either file type or file name:
     acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
     success: function (e, data) {
     },
     fail: function (e, data) {
     },
     done: function (e, data) {
     }
     }).on('fileuploadprocessalways', function (e, data) {
        var currentFile = data.files[data.index];
        if (data.files.error && currentFile.error) {
          // there was an error, do something about it
          console.log(currentFile.error);
        }
    });

如果我有这个,那么将捕获错误的文件结尾并停止文件上传,但用户无法主动开始上传。

我还尝试过fileuploadprocessalways的代码,但currentFiledata.files.error未定义。

add: function (e, data) {
     $uploadButton.show();
     $uploadButton.on('click', function () {
         var currentFile = data.files[data.index];
         if (data.files.error && currentFile.error) {
              // Here no error occurs ever, both conditions are undefined 
              console.log(currentFile.error);
        } else {
            data.submit();

     });
},

0 个答案:

没有答案