简体   繁体   English

从多个文件上传中检索文件大小

[英]Retrieve file size from multiple file uploads

I have this code below 我下面有这段代码

$("#btn_save_development").bind("click", function () {


    var devMainImgSize = parseFloat($("#dev_main_image")[0].files[0].size / 1024).toFixed(2);
    var devListingImg1Size = parseFloat($("#devListingImg1")[0].files[0].size / 1024).toFixed(2);
    var devListingImg2Size = parseFloat($("#devListingImg2")[0].files[0].size / 1024).toFixed(2);
    var devListingImg3Size = parseFloat($("#devListingImg3")[0].files[0].size / 1024).toFixed(2);
    var devListingImg4Size = parseFloat($("#devListingImg4")[0].files[0].size / 1024).toFixed(2);
    var devListingImg5Size = parseFloat($("#devListingImg5")[0].files[0].size / 1024).toFixed(2);
    var devListingImg6Size = parseFloat($("#devListingImg6")[0].files[0].size / 1024).toFixed(2);

    if(devMainImgSize > 800)
    {
        alert("The Main Listing Image too large. It should not be above 800KB. Please upload an image with a smaller size");
        return false;
    }

    if (devListingImg1Size > 800 || devListingImg2Size > 800 || devListingImg3Size > 800 || devListingImg4Size > 800 || devListingImg5Size > 800 || devListingImg6Size > 800) {
        alert("One or all of the Development Listing Image(s) is above 800KB. Please ensure none of the development listing images are larger than 800KB");
        return false;
    }
});

which i want to use to check the size of images being uploaded via the file uploads. 我想用来检查通过文件上传上传的图像的大小。 The problem i am having is, if i try to check the size of file in just one file upload the code works and i see the Javascript alert but when i try to check for multiple files i get this error in my browser console Uncaught TypeError: Cannot read property 'size' of undefined . 我遇到的问题是,如果我尝试仅检查一个文件中的文件大小,则代码可以正常工作,并且我看到Javascript警报,但是当我尝试检查多个文件时,我在浏览器控制台中收到此错误Uncaught TypeError: Cannot read property 'size' of undefined My question is, how can i check the file sizes of all the files in my file uploads? 我的问题是,如何检查文件上传中所有文件的文件大小? Thanks in advance 提前致谢

The issue is that if you do not have all of the input uploads filled with an image, then this error gets thrown. 问题是,如果您没有用图像填充所有input上载,则会引发此错误。 If you fill all of them, the error won't occur. 如果将它们全部填满,则不会发生错误。 You can create a function that checks whether the inputs are filled. 您可以创建一个检查输入是否已填充的函数。 Then if they aren't filled return a value of 0 . 然后,如果未填充它们,则返回值0 Otherwise return the size for the rest of the calculations you perform. 否则,返回您执行的其余计算的大小。

See the fiddle: https://jsfiddle.net/3xLLjko9/3/ 看到小提琴: https : //jsfiddle.net/3xLLjko9/3/

$("#btn_save_development").bind("click", function () {

    var devMainImgSize = parseFloat(checkFilled($("#dev_main_image")) / 1024).toFixed(2);
    var devListingImg1Size = parseFloat(checkFilled($("#devListingImg1")) / 1024).toFixed(2);
    var devListingImg2Size = parseFloat(checkFilled($("#devListingImg2")) / 1024).toFixed(2);
    var devListingImg3Size = parseFloat(checkFilled($("#devListingImg3")) / 1024).toFixed(2);
    var devListingImg4Size = parseFloat(checkFilled($("#devListingImg4")) / 1024).toFixed(2);
    var devListingImg5Size = parseFloat(checkFilled($("#devListingImg5")) / 1024).toFixed(2);
    var devListingImg6Size = parseFloat(checkFilled($("#devListingImg6")) / 1024).toFixed(2);

    //FOR TESTING PURPOSES
    console.log(devMainImgSize);
    console.log(devListingImg1Size);
    console.log(devListingImg2Size);
    console.log(devListingImg3Size);
    console.log(devListingImg4Size);
    console.log(devListingImg5Size);
    console.log(devListingImg6Size);

    if(devMainImgSize > 800)
    {
        alert("The Main Listing Image too large. It should not be above 800KB. Please upload an image with a smaller size");
        return false;
    }

    if (devListingImg1Size > 800 || devListingImg2Size > 800 || devListingImg3Size > 800 || devListingImg4Size > 800 || devListingImg5Size > 800 || devListingImg6Size > 800) {
        alert("One or all of the Development Listing Image(s) is above 800KB. Please ensure none of the development listing images are larger than 800KB");
        return false;
    }
});

function checkFilled(ele){
  if(ele[0].files.length > 0){
    return ele[0].files[0].size;
  } else {
    return 0;
  }
}

Another approach could be to validate the files as the user selects them. 另一种方法可能是在用户选择文件时验证文件。 You could accomplish this by listening for changes on the input. 您可以通过侦听输入的更改来完成此操作。

$("#fileInput").on("change", processFiles);

var processFiles = function(event) {
  var files = event.target.files;
  //Loop through our files  
  for (var i = 0; i < files.length; i += 1) {
    var file = files[i]; 
    if(file.size / 1024 > 800){
          alert("file too big!");
    }
    //Build the File Info HTML  
    var fileInfo = ["<li>",
                     file.name,
                     "<span>",
                     file.size / 1024,
                     " kb</span>", 
                     "</li>"].join(''); 
    $("#list").append(fileInfo);
    reader.onload = loaded;
    reader.readAsDataURL(file);
  }
};

Here is a working fiddle. 这是一个工作的小提琴。

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

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