javascript - jQuery-File-Upload - 一种形式的多个输入文件

标签 javascript jquery jquery-plugins jquery-file-upload blueimp

插件“blueimp/jQuery-File-Upload”让我发疯。 在表单内,我有 2 个文件输入:一个用于图像,另一个用于文档。

<form id="file-upload">

<!--File 1 : Image -->
<input id="file-img" name="file1" type="file">
<input id="txt-file-img" type="text" class="form-control" style="background:white;" readonly>

<!--File 2 : Document -->
<input id="file-doc" name="file2" type="file">
<input id="txt-file-doc" type="text" class="form-control" style="background:white;" readonly>

</form>

我想根据用于选择文件的输入应用不同的测试(大小和类型)。 我在文档中找不到如何管理这种简单的情况。

$('#file-upload').fileupload({
    dataType: 'json',
    autoUpload: false,
    add: function (e, data) {
        var uploadErrors = [];
        var acceptImgTypes = /^image\/(gif|jpe?g|png)$/i;
        var acceptDocTypes = /^application\/(pdf|msword)$|^text\/plain$/i;

        /**TEST HERE**/
        /** How to make the distinction between data coming from file-img and data coming from file-doc ? ***/
        if(data.originalFiles[0]['type'].length && !acceptFileTypes.test(data.originalFiles[0]['type'])) {
            uploadErrors.push('Not an accepted file type');
        }
        if(data.originalFiles[0]['size'].length && data.originalFiles[0]['size'] > 4000000) {
            uploadErrors.push('File size is too big');
        }
        if(uploadErrors.length > 0) {
            alert(uploadErrors.join("\n"));
        } else {
        /** Same problem here I need to make the distinction  in order to update the right input[type=text]***/
            $.each(data.files, function (index, file) {
                $("#txt-file-img").val(file.name);
                $("#txt-file-doc").val(file.name);
            });

            $("#btn_add_valid").on('click',function () { 
                $("#loading_modal").modal("show");  
                data.submit();
             });
        }
    },
    done: function (e, data) {
        $("#loading_modal").modal("hide");
        $("#output").html('<p class="sucess">OK!</p>');
    },
    progressall: function (e, data) {
        var progress = parseInt(data.loaded / data.total * 100, 10);
        $('#upload-progress .bar').css(
            'width',
            progress + '%'
        ).text(
            progress + '%'
        );
    },
    fail: function (e, data) {
        $("#output").html('<p class="error">FAIL!</p>');
    }
});

您知道如何使用此插件管理多个输入[type=file]吗?

非常感谢您的帮助

最佳答案

我找到了一个带有输入参数“name”的解决方案:

如果您的输入有 2 个不同的名称(例如:file1 和 file2),您可以通过以下测试知道所选文件的来源:

if (data.paramName=="file1")
{
    .....
}else{
    ....
}

所以我对类型和尺寸做了如下测试:

var uploadErrors = [];
var acceptImgTypes = /^image\/(gif|jpe?g|png)$/i;
var acceptDocTypes = /^application\/(pdf|msword|vnd.openxmlformats-officedocument.wordprocessingml.document)$|^text\/plain$/i;


if (data.paramName=="file1")
{
    if (data.originalFiles[0]['type'] && !acceptImgTypes.test(data.originalFiles[0]['type'])) 
    {
        uploadErrors.push('Not an accepted file type');
    }
    if (data.originalFiles[0]['size'] && data.originalFiles[0]['size'] > 4000000) 
    {
        uploadErrors.push('File size is too big');
    }
}
else
{
    if (data.originalFiles[0]['type'] && !acceptDocTypes.test(data.originalFiles[0]['type'])) 
    {
        uploadErrors.push('Not an accepted file type');
    }
    if (data.originalFiles[0]['size'] && data.originalFiles[0]['size'] > 10000000) 
    {
        uploadErrors.push('File size is too big');
    }
}

与 inputTxt 相同(带有文件名)

if (data.paramName=="file1")
{
    $("#txt-file-img").val(data.files[0].name);
}
else
{
    $("#txt-file-doc").val(data.files[0].name);
}

测试没问题,但现在我在上传过程中遇到错误:

"error":"File upload aborted"
"deleteType":"DELETE"

你明白为什么吗?

非常感谢

关于javascript - jQuery-File-Upload - 一种形式的多个输入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26822869/

相关文章:

用于旋转和调整图像大小的 Jquery 插件

javascript - 如何使用javascript/jquery获取隐藏字段的值

javascript - 在面向对象的 Javascript 文件上使用 jQuery

javascript - 有没有办法在将 .ejs 文件渲染到 .html 文件的同时运行函数?

javascript - 如何在重新初始化之前销毁 JQuery 插件实例? (照片刷卡)

javascript - jquery/javascript 供用户选择 n 个图像中的 1 个

javascript - 在 Angular 中将 attrs 作为 $scope 变量传递

javascript - 表单字段依次输入

javascript - jquery 检查数字是否在列表中

javascript - 如何创建终极 jquery 脚本来跟踪 Google Analytics 中的出站链接?