JavaScript javascript FileReader get name? javascript filereader file name,javascript filereader get name,javascript filereader

I'm working on a simple script for my site to upload images. I have a multiple file input <input type = 'file' name = 'files[]' id = 'hiddenFile' multiple> that is being triggered by a div click. When I queue the files, I want to be able to delete one. I know I can loop through the $('#hiddenFile').val() and splice to get the name out but I'm having an issue with figuring out the file name. When I assign the file to a new img container, how do I get the name? I've tried console.log(f.name) and a few variations but it returns an undefined error. Here are my scripts. I think I'm pretty close but this is something I'm learning as I go. Thanks!

function readURL(input) {
var files = $('#hiddenFile')[0].files; //where files would be the id of your multi file input
//or use document.getElementById('files').files;

    for (var i = 0, f; f = files[i]; i++) {

        var reader = new FileReader();

        reader.onload = function (e) {
            console.log(f.name); //how can I get the 
                                 //file name here to add it to the image as an attribute?
            $("<img src = '"+e.target.result+"' class = 'addedImg'>").appendTo("#imgBox");
        };

        reader.readAsDataURL(f);

    }
}

$(document).ready(function(){
    $(document).on('click', '#addBtn', function(e){
        e.preventDefault();
        $('#hiddenFile').click();
    });
});

enter image description here

Answer:1

Try using change event , defining f within an IIFE , setting title attribute value to f.name

$(document).ready(function() {

  $(document).on('click', '#addBtn', function(e) {
    e.preventDefault();
    $('#hiddenFile').click();
  });

  $("#hiddenFile").change(function(event) {
    var files = this.files; 
    var i = 0,
      len = files.length;
    (function readFile(n) {
      var reader = new FileReader();
      var f = files[n];
      reader.onload = function(e) {
        console.log(f.name);
        $("<img src=" + e.target.result + " class=addedImg title=" + f.name + ">")
        .appendTo("#imgBox");
        // if `n` is less than `len` , 
        // call `readFile` with incremented `n` as parameter
        if (n < len -1) readFile(++n)
      };
      reader.readAsDataURL(f); // `f` : current `File` object
    }(i)); // `i` : `n` within immediately invoked function expression
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div id="addBtn">click</div>
<input type="file" style="display:none" id="hiddenFile" multiple />
<div id="imgBox"></div>
Answer:2

I am working on some pretty old code and in every jsp file where there is a javaScript block I see some bizarre Syntax. It goes like : <script language="JavaScript"> <!-- here is the ...

I am working on some pretty old code and in every jsp file where there is a javaScript block I see some bizarre Syntax. It goes like : <script language="JavaScript"> <!-- here is the ...

I been trying to do something like this, where clicking on the list links to a different address("#/tab/cases/case-detail/{case.ID}") and clicking on the play button goes to another address("#/tab/...

I been trying to do something like this, where clicking on the list links to a different address("#/tab/cases/case-detail/{case.ID}") and clicking on the play button goes to another address("#/tab/...

  1. angular repeat item

I have a problem with my code, namely: If in input #search_code I introduce letter 'm' and in #insert_code input I introduce letter "M", function returns "is not ok". I tried to make uppercase ...

I have a problem with my code, namely: If in input #search_code I introduce letter 'm' and in #insert_code input I introduce letter "M", function returns "is not ok". I tried to make uppercase ...

  1. make input field rounded corners
  2. make input fields responsive
  3. make input fields read only
  4. make input fields mandatory
  5. make pdf input fields
  6. how to make input field rounded
  7. how to make input fields mandatory in html
  8. how to make input fields required
  9. how to make input fields in word
  10. how to make input fields in excel
  11. jquery make all input fields readonly

Is there a way I can filter on an array, but omit certain key:values using lodash? For example: var people = [{ _id: 0, name: 'Joe', type: 1 }, { _id: 1, name: 'James', type: ...

Is there a way I can filter on an array, but omit certain key:values using lodash? For example: var people = [{ _id: 0, name: 'Joe', type: 1 }, { _id: 1, name: 'James', type: ...

  1. lodash filter exclude
  2. lodash filter out
  3. lodash filter or