Angular File Upload

后端 未结 12 1986
名媛妹妹
名媛妹妹 2020-11-22 10:04

I\'m a beginner with Angular, I want to know how to create Angular 5 File upload part, I\'m trying to find any tutorial or doc, but I don\'t see anything a

12条回答
  •  隐瞒了意图╮
    2020-11-22 10:44

    Ok, as this thread appears among the first results of google and for other users having the same question, you don't have to reivent the wheel as pointed by trueboroda there is the ng2-file-upload library which simplify this process of uploading a file with angular 6 and 7 all you need to do is:

    Install the latest Angular CLI

    yarn add global @angular/cli
    

    Then install rx-compat for compatibility concern

    npm install rxjs-compat --save
    

    Install ng2-file-upload

    npm install ng2-file-upload --save
    

    Import FileSelectDirective Directive in your module.

    import { FileSelectDirective } from 'ng2-file-upload';
    
    Add it to [declarations] under @NgModule:
    declarations: [ ... FileSelectDirective , ... ]
    

    In your component

    import { FileUploader } from 'ng2-file-upload/ng2-file-upload';
    ...
    
    export class AppComponent implements OnInit {
    
       public uploader: FileUploader = new FileUploader({url: URL, itemAlias: 'photo'});
    }
    

    Template

    
    

    For better understanding you can check this link: How To Upload a File With Angular 6/7

提交回复
热议问题