【问题标题】:jQuery File Upload - get size of uploaded imagesjQuery File Upload - 获取上传图片的大小
【发布时间】:2017-07-25 03:47:10
【问题描述】:

我正在使用 jQuery File Upload 来上传产品图片。每种产品都有不同“类型”的图片(不同的尺寸/尺寸)。

我已更改上传模板,以便为添加的每个文件显示可能的图像类型的下拉列表。在点击上传之前,用户必须从下拉列表中选择要上传的文件是什么图像类型。

有些图片类型是精确尺寸,有些只有最小宽度。我想要做的是将实际图像大小与选择的类型进行比较,以验证它们是否指示正确的类型。

我正在尝试通过“fileuploadsubmit”回调执行此操作,其中我已经添加了一个检查以确保在下拉列表中选择了一个选项。

$('#fileupload').bind('fileuploadsubmit', function (e, data) {
    $('#uploading_note').removeClass("alert-danger").html("");
    var inputs = data.context.find(':input');
    if (inputs.filter(function () {
        return !this.value && $(this).prop('required');
    }).first().focus().length) {
        data.context.addClass("alert-danger");
        data.context.find('.file_msg').html("Image Type is required!"); 
    data.context.find('button').prop('disabled', false);
    return false;
}
    var fixed_dimension = 0;
    var wt = 0;
    var ht = 0;
    var image_size = getImageSize(data,function(width, height) {
        wt = width;
        ht = height;
        console.log (wt+'x'+ht);
        return [wt,ht];
    }); 
   ........... 

这是 getImageSize 函数。

    function getImageSize(data,callback){
    var img = new Image();
    img.src = _URL.createObjectURL(data.files[0]);
            img.onload = function () {
                var w = this.width;
                var h = this.height;
                console.log(w+'-getImageSize-'+h);
                callback (w,h);
            }; 

}

两个输出中的 console.log() 都正确,但我无法在函数之外获取这些数字来使用它们。我知道它与异步功能有关,但我不知道如何解决它。或者也许我缺少一个更好的 jQuery File Upload 内置选项。

我了解异步函数问题,这是试图找到验证图像大小的解决方案。我也尝试在 PHP 脚本中执行此操作。它的作用是我可以阻止上传图像,但用户体验很差。所以我仍在寻找如何使用 jQuery File Upload 进行图像大小验证的解决方案。

以下是代码的更新版本。

$('#fileupload').bind('fileuploadsubmit', function (e, data) {
    console.log(data);

    $('#uploading_note').removeClass("alert-danger").html("");
    var inputs = data.context.find(':input');
    if (inputs.filter(function () {
        return !this.value && $(this).prop('required');
    }).first().focus().length) {
        console.log(data.context);
        data.context.addClass("alert-danger");
        data.context.find('.file_msg').html("Image Type is required!"); 
        //$('#uploading_note').addClass("alert-danger").html("Image Type is required!");
    data.context.find('button').prop('disabled', false);
    return false;
}

    var fixed_dimension = 0;
    var wt = 0;
    var ht = 0;
    var image_size = getImageSize(data,function(width, height) {
        wt = width;
        ht = height;
        console.log(wt+'-getImageSize-function'+ht);
       switch (inputs[0].value){
        case "16x9_background":
        case "16x9":
            var req_width = 1920;
            var req_height = 1080;
            fixed_dimension = 1;
            break;
        case "3x4":
            var req_width = 1600;
            var req_height = 1200;
            fixed_dimension = 1;
            break;
        case "hires":
            var req_width = 1400;
            var req_height = 0;
            break;
        case "lowres":
            var req_width = 200;
            var req_height = 0;
            break;
    }
    if (fixed_dimension){
        if (wt != req_width || ht != req_height){
            data.context.addClass("alert-danger");
            data.context.find('.file_msg').html("Image dimensions must be exactly "+req_width+"x"+req_height+". This image is "+wt+"x"+ht+". Image not uploaded.");
            return false;
        } 
    } else {
        if (wt < req_width){
           data.context.addClass("alert-danger"); 
           data.context.find('.file_msg').html("Image dimensions wrong!! Width must be "+req_width+", not "+wt+". Image not uploaded.");
            console.log(wt+'x'+ht);
            return false;
        }
    }    
    d = inputs.serializeArray();
    d.push({ name: "folder", value: $('#barcode').val() });
    d.push({ name: "stock_num", value: $('#stock_num').val() });
    data.formData = d;
    console.log(wt+"xxxx"+ht);
    data.submit();  // SUMBIT THE FORM
    }); 

    console.log(wt+"xxxx"+ht);
    console.log("imagesize:"+image_size);
    return false;  //STOP THE DEFAULT SUBMIT
}); 

【问题讨论】:

  • 如何从异步回调函数返回值的可能重复:stackoverflow.com/questions/6847697/…
  • 同意@kmoser,你问题的根本原因是你没有正确处理异步代码,但你可能也对this Q/A感兴趣。
  • 是的,我知道问题与异步代码有关,我还没有弄清楚如何正确编码,我会阅读这些链接。但我也真的希望有一个 jQuery 文件上传我忽略的特定解决方案。
  • 另外要清楚,我只需要获取上传图像的大小。我不希望它是异步的,但似乎这个 img.onload 函数是异步完成的。如何强制它不是异步的?

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


【解决方案1】:

我最终做的是删除实际的表单提交按钮并将其替换为调用验证代码的按钮(不是类型提交)。如果通过,则正常提交表单(通过 data.submit()),否则返回错误。我不得不删除文件级提交按钮(或者我发现这样做更容易)。

    $('#fileupload').fileupload({
    url: 'process.php',
    previewThumbnail: true,
    sequentialUploads: true,
    acceptFileTypes: /(\.|\/)(jpe?g|pdf|zip)$/i,
    maxChunkSize: 10000000,
    add: function (e, data) {
        $('#start_upload_button').click(function () {
          getImageSize3(data).then(function(dimensions) {
        image validation... return false if any errors

    data.submit() // submit the form
    }); 

【讨论】:

    猜你喜欢
    • 2018-06-02
    • 2018-02-11
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多