繁体   English   中英

使用jquery文件上传验证文件?

[英]Validate files using jquery file upload?

我正在使用jquery文件上传,如下所示。

dialogElem.find('#upload-image-file-input').fileupload({
            url: url,
            dataType: 'json',
            autoUpload: true,
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            maxFileSize: 5000000, // 5 MB
            // Enable image resizing, except for Android and Opera,
            // which actually support image resizing, but fail to
            // send Blob objects via XHR requests:
            disableImageResize: /Android(?!.*Chrome)|Opera/
                .test(window.navigator.userAgent),
            previewMaxWidth: 100,
            previewMaxHeight: 100,
            previewCrop: true
        }).on('fileuploadadd', function (e, data) {
            var fileCount = data.originalFiles.length;
            if (fileCount > 5) {
                alert("The max number of files is : "+5);
                return false; 
            }
          }).on('fileuploadprocessalways', function (e, data) {
              //some logic
           }).on('fileuploadprogress', function (e, data) {
              //some logic
           }).on('fileuploaddone', function (e, data) {
              //some logic
            }).on('fileuploadfail', function (e, data) {
              //some logic
            })

fileuploadadd内部,我添加了一些验证逻辑。 如果验证失败,如何停止所有其他事件,例如fileuploadprogressfileuploadfailfileuploadprocessalways

如果您有一些像我几周前一样的任务,请尝试以下操作:

您可以通过中止XHR(ajax请求)调用来取消正在进行的上传。 您可以绑定到文件输入字段的fileuploadadd事件,在保留jqXHR对象的同时提交请求,并将其用于中止:

jqXHR.abort();

例如:

$(".cloudinary-fileupload").bind('fileuploadadd', function(e, data) {
    jqXHR = data.submit(); // Catching the upload process of every file
});

$('#cancel_button').click(function (e) {
    if (jqXHR) {
        jqXHR.abort();  
        jqXHR = null;
        console.log("Canceled");
    }
    return false;
});

下一页包含更多代码示例https://github.com/blueimp/jQuery-File-Upload/issues/290

只是打电话

if (fileCount > 5) { 
jqXHR.abort();  
jqXHR = null; }

jqXHR.abort()对我不起作用。 但是throw中止“添加”处理:

.bind('fileuploadadd', function (e, data) {
    if (total_uploads >= maxImagesPerPost){
        throw new Error('Upload maximum reached');
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM