2

I have a dynamic inputs of file in html so i add each file in array of file

   tmp_files :File[] = [] ;

and in HTML

 <div class="container">
 <input type="file" (change)="fileChange($event)" name="file" id="file" class="inputfile">

in component filechange function look like this

fileChange(event) {
this.tmp_files.push(event.target.files);}

and save function

save(){
 for (let i = 0; i < this.tmp_files.length; i++) {
        console.log("ccc",this.tmp_files[i]);

      }
}

So in console log i get files enter image description here

So now i tried to get File and File name, i spent a lot of time , i tried a lot of thing, but i can't resolve this issue so thanks to help me to Get File and her name because i want to send this files to server thanks again

1
  • Can you console.log("ccc", JSON.stringify(this.tmp_files)) before the loop instead? It's difficult to tell what exactly is going on from a screenshot of an object... Commented May 15, 2017 at 21:36

1 Answer 1

3

Your File type is an object with the first numerical index being the File itself. Not sure how this happened, but it might be a good idea to flatten out (Or maybe that's just Typescript's browser typing, I don't know).

You would get hold of it like so:

save() {
  for (let i = 0; i < this.tmp_files.length; i++) {
        console.log('File:', this.tmp_files[i][0], 'Name:', this.tmp_files[i][0].name);
      }
}

or more cleanly in ES6 standards:

save(filename: string) {
    const myFile = this.tmp_files.find(s => s[0].name === filename);
    // do stuff with MyFile
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.