【问题标题】:Find size of an image in javascript without using file property as it is not supported in eclipse在不使用文件属性的情况下在 javascript 中查找图像的大小,因为 eclipse 不支持它
【发布时间】:2014-12-16 05:56:19
【问题描述】:

![在此处输入图片描述][1]我想在通过 javascript 提交表单之前验证图片大小。但是在eclipse中:

var filesize=filename.files[0].size 

其中文件名是存储图像路径的 var 不起作用。

不支持。任何人都可以帮我找到解决方案或告诉我在提交表单之前检查客户端图像大小的任何其他替代方法

【问题讨论】:

  • filename 必须是 HTMLInputElement 才能使您的代码有意义

标签: javascript eclipse filesize


【解决方案1】:

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_fileupload_files

<!DOCTYPE html>
<html>
<body onload="validateFileSize()">
<input type="file" id="myFile"  onchange="validateFileSize()">
<p id="demo"></p>
<script>
var maxSizeBytes=10000;
function validateFileSize(){
    var x = document.getElementById("myFile");
    var txt = "";
    if ('files' in myFile) {
        var file = x.files[0];
        if ('size' in file) {
                    if(file.size >maxSizeBytes)
                     alert("Invalid file size.Maximum allowed size is "+maxSizeBytes+" bytes");
            txt += "size: " + file.size + " bytes <br>";
         }
    }    
    document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>

【讨论】:

  • 先生,我正在 eclipse indigo 中制作这个项目
  • 您是在 Eclipse 内部浏览器中检查您的应用程序吗?这是 Web 应用程序还是移动混合应用程序?
  • 是的,先生,我正在使用 Eclipse 的内部浏览器。这是一个网络应用程序
  • Eclipse 内部浏览器不支持文件输入类型,但此代码在所有主要桌面浏览器(IE10+、Chrome 所有版本、FF31+、Safari 5.1+、Opera 26+)中都能正常工作。它会是如果您可以使用真正的桌面浏览器进行测试,那就更好了,因为最终您的应用程序将在其中一个上运行。检查这个caniuse.com/#feat=input-file-multiple
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-23
  • 2020-08-07
  • 1970-01-01
  • 2017-08-03
  • 2021-12-25
相关资源
最近更新 更多