0

Using Chrome and Internet Explorer I am finding the following problem related how to obtain the file name inserted into an input tag using jQuery.

So I have the following situation: into a page I have this input tag:

<input id="rendicontoAllegato" class="form-control" style="height: 30px; padding: 0px;" type="file">

And a div having id="nomeDocumentoRendicontazione" that will contain the name of the file selected into the previous input tag:

<div id="nomeDocumentoRendicontazione"></div>

And associated to the change event of this input file I have this jQuery script:

$(document).ready(function() {

    // When the user select a new fine into the input tag:
    $("#rendicontoAllegato").change(function() {

        // Retrieve the name of the selected file and put it into the hidden div having id="nomeDocumentoRendicontazione":
        var selectedFileName = $("#rendicontoAllegato").val();          
        $('#nomeDocumentoRendicontazione').text(selectedFileName);
    });
 });

As you can see the previous script when the user select a new fine into the input tag change the value into the div having id="nomeDocumentoRendicontazione".

Using FireFox it works fine and if I select a file name cat.jpg in the div I have the value cat.jpg.

But if I use Chrome or Internet Explorer, after the file selection in the div I have the following value C:\fakepath\cat.jpg

Why? How can I obtain only the file name and not the **C:\fakepath** before it using Chrome and Internet Explorer

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

2 Answers2

0

Just remove the prefix if it's there:

    var selectedFileName = $("#rendicontoAllegato").val()
        .replace(/^C:.*\\/, '');          
Pointy
  • 405,095
  • 59
  • 585
  • 614
0

That's because of security reasons... Following question has some fine answers:

How to resolve the C:\fakepath?

Community
  • 1
  • 1
Jeroen Bellemans
  • 2,049
  • 2
  • 25
  • 42