I'm beginner for the Angular, I want to know how to create Angular-5
File upload part, I'm try to find any tutorial or doc, but I don't see anything anywhere,Any Idea for this? and I'm try to that ng4-files but its not work for Angular-5
相关问题
- Angular RxJS mergeMap types
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- How to update placeholder text in ng2-smart-table?
- How to instantiate Http service in main.ts manuall
- Angular: ngc or tsc?
相关文章
- angular脚手架在ie9+下兼容问题
- angular 前端项目 build 报错 "Cannot find module 'le
- Angular Material Stepper causes mat-formfield to v
- After upgrade to Angular 9 cannot find variable in
- is there any difference between import { Observabl
- Suppress “Circular dependency detected” suppress w
- How can you get current positional information abo
- Angular material table not showing data
I am using Angular 5.2.11, I like the solution provided by Gregor Doroschenko, however I noticed that the uploaded file is of zero bytes, I had to make a small change to get it to work for me.
The following lines (formData) didn't work for me.
https://github.com/amitrke/ngrke/blob/master/src/app/services/fileupload.service.ts
Very easy and fastest method is using ng2-file-upload.
Install ng2-file-upload via npm.
npm i ng2-file-upload --save
At first import module in your module.
Markup:
In your commponent ts:
It`is simplest usage of this. To know all power of this see demo
Try this
Install
Import
Html
Here is a working example for file upload to api:
Step 1: HTML Template (file-upload.component.html)
Define simple input tag of type
file
. Add a function to(change)
-event for handling choosing files.Step 2: Upload Handling in TypeScript (file-upload.component.ts)
Define an default variable for selected file.
Create function which you use in
(change)
-event of your file input tag:If you will to handle multifile selection, than you can iterate through this files array.
Now create file upload function by calling you file-upload.service:
Step 3: File-Upload Service (file-upload.service.ts)
By uploading a file via POST-method you should use
FormData
, because so you can add file to http request.So, This is very simple working example, which I use everyday in my work.