How to get the path of a field type file

1

Hi, I would like help with the following problem:
I have a form with the upload field of type file and would like to get the file path that is selected and save it in a variable with javascript:

 <form action="/upload" enctype="multipar/form-data" method="POST"> 
     <div><input type="file" name="upload"></div>
     <div><input type="submit" "up"></div>
 </form>
    
asked by Edgar Castillo 29.08.2017 в 18:24
source

2 answers

1

You can use jquery and greatly simplify what you want in this way: (I put an image to show that the route is good but you can upload any file)

$(document).ready(function() {

  // Escuchamos el evento 'change' del input donde cargamos el archivo
  $(document).on('change', 'input[type=file]', function(e) {
    // Obtenemos la ruta temporal mediante el evento
    var TmpPath = URL.createObjectURL(e.target.files[0]);
    // Mostramos la ruta temporal
    $('span').html(TmpPath);
    $('img').attr('src', TmpPath);
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" name="upload"><br /><br />
<span></span><br /><br />
<img width="200" alt="Imagen" />
    
answered by 29.08.2017 в 18:45
0

For security reasons the browser does not have access to the File System, meaning that this path can not be obtained normally. Searching the web says that only with Mozila Firefox is possible but obviously NOT recommended.

In the following link you can find more information and some suggestions: link

    
answered by 29.08.2017 в 18:44