Javascript button error on IE9

Asked

Viewed 57 times

0

I have a browser problem only in IE9, which occurs at the time I load an attachment and when I click the include attachment button (POST) as image below: inserir a descrição da imagem aqui

function uploadResponse(frameId) {

    $.ajax({
        type: "POST",
        url: "/Turma/AtualizarGridAnexo",
        success: function (data) {
            $("#divGridAnexo").html(data);//Fill div with results
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr);
            alert(ajaxOptions);
            alert(thrownError);
        }
    });


}

$("#adicionarAnexo").click(function (e) {


    var fileInput = $('#idAnexo');
    var maxSize = 5000000; //colocar valor em bytes
    var fname = $('#idAnexo[type=file]');
    console.log(fileInput.get(0).files[0].size);

    //pega o tamanho do arquivo
    if (fileInput.get(0).files.length) {
        var fileSize = fileInput.get(0).files[0].size;
        //verifica se é maior que 5MB
        if (fileSize > maxSize) {
            alert('Este arquivo é muito grande para ser anexado. Limite máximo permitido: 5MB.');
            return false;
        }
        //verifica se a extensão do arquivo é permitida
        var fileName = $('input[type=file]').val().toUpperCase();
        var regex = new RegExp("(.*?)\.(PDF|XLS|XLSX|DOC|DOCX|PPT|PPTX|MP3|WMA|AVI|PNG|JPG)$");
        if (!(regex.test(fileName))) {
            $('input[type=file]').val('');
            alert('Extensão não permitida, as extensões permitidas são: PDF, XLS, XLSX, DOC, DOCX, PPT, PPTX, MP3, WMA, AVI, PNG e JPG');
            return false;
        }
        //se o tamanho e a extensão for correta, faz upload do arquivo
        else {
           var tempFrame = document.createElement("iframe");
           tempFrame.src = "";
           tempFrame.onload = uploadResponse.bind(this, tempFrame);
           tempFrame.name = "tmpFrameUpload"
           this.appendChild(tempFrame);
           this.form.target = tempFrame.name;
           this.form.name = "uploadForm";
           this.form.acceptCharset = "UTF-8";
           //This is an example of a hidden input, used to pass extra vars to the server. Add more if you need them.
           //var tempNodePath = document.createElement("input");
           //tempNodePath.type = "hidden";
           //tempNodePath.value = [dir]; //if you want specify a target path.
           //tempNodePath.name = "filePath";
           //this.form.insertBefore(tempNodePath, this.form.childNodes[0]);
           this.form.submit();
           jq.idAnexo.val('');
        }
    } else {
        return false;
    }
});

The error occurs in the console log.(fileInput.get(0). files[0]. size);

Could someone help me? I’ve searched here but found nothing like it...

From now on I thank you guys!!

Console debug... inserir a descrição da imagem aqui

  • You are trying to access an Index in files before checking if there is a file, try to move the console.log(fileInput.get(0).files[0].size); into the block if (fileInput.get(0).files.length) { ... }

  • @Tobymosque He’s already there, but no console log..

  • @Mariane, you debug through the console and checked the data of each variable?

  • @Guilhermelautert already yes, I just did that and I saw that it is my input[type=file]'). which is not supported by the IE9 browser. But I don’t know how to change this, what to put in place of input[type=file]').

  • @Guilhermelautert I edited the question by adding more console debug information...

1 answer

4


Mariane, the API for accessing files on a input[type='file'] is not implemented in IE9.

Below is the list of browsers that support:

  • Chrome 13
  • Firefox 7
  • Internet Explorer 10
  • Opera 16
  • Safari (Webkit) 6

More information on File API and Can I Use

As an option, you can use some blibioteca that emulates a input[type='file'] like the Fileapi, or invite the user to update the Browser with the Outdated Browser v1.1.1.

  • I edited the question by adding more console debug information...

  • I cannot see the images on Imgur.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.