Pergunta

Se eu tiver um

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

tag e um botão de envio, como determino, no IE6 (e superior) se um arquivo foi selecionado pelo usuário.

No FF, eu apenas faço:

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

Mas isso não funciona no IE.

Foi útil?

Solução

Isso funciona no IE (e no FF, acredito):

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

Outras dicas

este pedaço de código funciona no meu ambiente local, espero que também funcione ao vivo

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

Chame esta função em change .

Você pode usar:

    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) }

Todos os três dão o mesmo resultado.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top