Get file name in input file

Asked

Viewed 14,517 times

2

I have an input, where when I click, I want you to enter the file name.

I have the following code:

HTML

<div class="trabalheObs">Carregar Curr&iacute;culo</div>
<input style="display:none" type="file" class="margin-top-20 cp" id="curriculoForm" name="curriculoForm" value="" />

CSS

.trabalheObs {
        background-image: url("../imagens/curriculo.png");
        background-position: 420px center;
        background-repeat: no-repeat;
        margin-top: 20px;
        width: 460px;
        height: 45px;
        border-top: 1px solid #e6e6e6;
        background-color: white;
        border-radius: 25px;
        text-indent: 23px;
        font-family:"roboto";
        font-weight: 300;
        font-style: italic;
        color: #999999;
        font-size: 18px;
    }

JQUERY

$(".trabalheObs").click(function () {
    $("#curriculoForm").click();
});

If you notice, I put a DIV top, to mask the standard ugly layout of a input. However, I would like him to inform the name of the selected file, how to do this?

I want the file name inside div trabalheObs

  • Related/duplicate: http://answall.com/q/38877/129

2 answers

4

In pure Javascript, you can get the file name by manipulating the Fileupload Input object, as an example below:

var div = document.getElementsByClassName("botaoArquivo")[0];
var input = document.getElementById("inputArquivo");

div.addEventListener("click", function(){
    input.click();
});
input.addEventListener("change", function(){
    var nome = "Não há arquivo selecionado. Selecionar arquivo...";
    if(input.files.length > 0) nome = input.files[0].name;
    div.innerHTML = nome;
});
#inputArquivo { display: none; }
<div class="botaoArquivo">Selecionar arquivo...</div>
<input type="file" id="inputArquivo">

3


You can get the selected file name by obtaining the field value.

In this specific case, using the following code:

var nomeDoArquivo = $("#curriculoForm").val(); // obtem o nome
$(".trabalheObs").html(nomeDoArquivo); // coloca o nome dentro da div

// ao clicar na div, dispara a acao do botao escondido
$(".trabalheObs").click(function(){
    $("#curriculoForm").click();
})
  • And how do I enter the name inside the div trabalheObs?

  • @Felipestoker I edited my answer. See if it helps you.

  • The content of div is the same? <div class="workObs">Load Curr&iacute;culo</div>

  • @Felipestoker I didn’t notice that Oce was using class instead of ID. I edited my answer to use class.

  • Nor did I notice, hehe. Next, he informed the name inside the input, only that I left as display:none, would need you to inform within the div

  • 1

    Value includes the file path, which many browsers do not allow access to. In Chrome, for example, your code returns C:\fakepath\nome-do-arquivo.ext.

  • Is there any way I can inform that value within the div?

  • @Felipestoker I don’t know if I got it right, but Vee wants you to click on the div, open the screen to choose the file? If so, see the edit. I added a click event to the div that simulates a click on the hidden upload button.

  • This I had already done. What happens is that the input is hidden, with the div on top. I wanted the file name to be informed within the div and not in the input.

Show 4 more comments

Browser other questions tagged

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