I know this is very a general question but I am failing to upload a file in Angular 2. I have tried
1) http://valor-software.com/ng2-file-upload/ and
2) http://ng2-uploader.com/home
...but failed. Has anyone uploaded a file in Angular? What method did you use? How to do so? If any sample code or demo link is provided it will be really appreciated.
Try not setting the
options
parameterthis.http.post(${this.apiEndPoint}, formData)
and make sure you are not setting the
globalHeaders
in your Http factory.Since the code sample is a bit outdated I thought I'd share a more recent approach, using Angular 4.3 and the new(er) HttpClient API, @angular/common/http
In Angular 2+, it is very important to leave the Content-Type empty. If you set the 'Content-Type' to 'multipart/form-data' the upload will not work !
upload.component.html
upload.component.ts
Thanks to @Eswar. This code worked perfectly for me. I want to add certain things to the solution :
I was getting error :
java.io.IOException: RESTEASY007550: Unable to get boundary for multipart
In order to solve this error, you should remove the "Content-Type" "multipart/form-data". It solved my problem.
Today I was integrated ng2-file-upload package to my angular 6 application, It was pretty simple, Please find the below high-level code.
import the ng2-file-upload module
app.module.ts
Component ts file import FileUploader
app.component.ts
Component HTML add file tag
app.component.html
Working Online stackblitz Link: https://ng2-file-upload-example.stackblitz.io
Stackblitz Code example: https://stackblitz.com/edit/ng2-file-upload-example
Official documentation link https://valor-software.com/ng2-file-upload/
I have used the following tool from priming with success. I have no skin in the game with primeNg, just passing on my suggestion.
http://www.primefaces.org/primeng/#/fileupload