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

时间:2021-06-12 10:14:02

If I have a

如果我有

<input id="uploadFile" type="file" />

tag, and a submit button, how do I determine, in IE6 (and above) if a file has been selected by the user.

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

In FF, I just do:

在FF中,我只是这样做:

var selected = document.getElementById("uploadBox").files.length > 0;

But that doesn't work in IE.

但这在IE中不起作用。

4 个解决方案

#1


89  

This works in IE (and FF, I believe):

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

if(document.getElementById("uploadBox").value != "") {
   // you have a file
}

#2


5  

this piece of code works in my local environment, hope it will also works in live

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

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

#3


0  

function validateAndUpload(input){
    var URL = window.URL || window.webkitURL;
    var file = input.files[0];

    if (file) {
        var image = new Image();

        image.onload = function() {
            if (this.width) {
                 console.log('Image has width, I think it is real image');
                 //TODO: upload to backend
            }
        };

        image.src = URL.createObjectURL(file);
    }
};​

<input type="file" name="uploadPicture" accept="image/*" onChange="validateAndUpload(this);"/>

Call this function on change .

更改时调用此函数。

#4


0  

You can use:

您可以使用:

    var files = uploadFile.files;

    if (files.length == 0) { console.log(true) } else { console.log(false) }
    if (files[0] == undefined) { console.log(true) } else { console.log(false) }
    if (files[0] == null) { console.log(true) } else { console.log(false) }

All three give the same result.

这三个都给出了相同的结果。

#1


89  

This works in IE (and FF, I believe):

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

if(document.getElementById("uploadBox").value != "") {
   // you have a file
}

#2


5  

this piece of code works in my local environment, hope it will also works in live

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

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

#3


0  

function validateAndUpload(input){
    var URL = window.URL || window.webkitURL;
    var file = input.files[0];

    if (file) {
        var image = new Image();

        image.onload = function() {
            if (this.width) {
                 console.log('Image has width, I think it is real image');
                 //TODO: upload to backend
            }
        };

        image.src = URL.createObjectURL(file);
    }
};​

<input type="file" name="uploadPicture" accept="image/*" onChange="validateAndUpload(this);"/>

Call this function on change .

更改时调用此函数。

#4


0  

You can use:

您可以使用:

    var files = uploadFile.files;

    if (files.length == 0) { console.log(true) } else { console.log(false) }
    if (files[0] == undefined) { console.log(true) } else { console.log(false) }
    if (files[0] == null) { console.log(true) } else { console.log(false) }

All three give the same result.

这三个都给出了相同的结果。