1

How can I send a file image in angular2 to my backend? I have this in my component.html:

<input type="file" [(ngModel)]="selectedImage" />
<button  (click)="uploadImage($event, selectedImage)">Save</button>

but in my function, selectedImage is undefined.

uploadImage($event, file) {
    console.log(file); //file is undefined
}
1

2 Answers 2

1

You can use

<div>
    <input type="file" (change)="onChange($event)"/>
</div>

and in the JavaScript:

onChange(event) {
    var files = event.srcElement.files;
    console.log(files);
}

Do not forget to

import {Component, EventEmitter} from '@angular/core';
Sign up to request clarification or add additional context in comments.

Comments

0

You can use ng2-file-upload!

npm install ng2-file-upload --save

Link here: http://valor-software.com/ng2-file-upload/

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.