如何确定用户是否选择了文件上传文件?

slo*_*ife 54 html javascript upload

如果我有

<input id="uploadFile" type="file" />
Run Code Online (Sandbox Code Playgroud)

标记和提交按钮,如何在IE6(及更高版本)中确定用户是否选择了文件.

在FF中,我只是这样做:

var selected = document.getElementById("uploadBox").files.length > 0;
Run Code Online (Sandbox Code Playgroud)

但这在IE中不起作用.

Jas*_*ing 106

这适用于IE(和FF,我相信):

if(document.getElementById("uploadBox").value != "") {
   // you have a file
}
Run Code Online (Sandbox Code Playgroud)


小智 7

这段代码在我的本地环境中工作,希望它也能在现场工作

var nme = document.getElementById("uploadFile");
if(nme.value.length < 4) {
    alert('Must Select any of your photo for upload!');
    nme.focus();
    return false;
}
Run Code Online (Sandbox Code Playgroud)


Ash*_*shu 5

function validateAndUpload(input){\n    var URL = window.URL || window.webkitURL;\n    var file = input.files[0];\n\n    if (file) {\n        var image = new Image();\n\n        image.onload = function() {\n            if (this.width) {\n                 console.log(\'Image has width, I think it is real image\');\n                 //TODO: upload to backend\n            }\n        };\n\n        image.src = URL.createObjectURL(file);\n    }\n};\xe2\x80\x8b\n\n<input type="file" name="uploadPicture" accept="image/*" onChange="validateAndUpload(this);"/>\n
Run Code Online (Sandbox Code Playgroud)\n\n

在发生变化时调用此函数。

\n