Fireship · @fireship

Angular Drag-and-Drop File Uploads to Firebase Storage

Uploaded June 24, 2017 · Length 6:15

Angular Drag-and-Drop File Uploads to Firebase Storage

Summary

build drag and drop file upload feature with angular and firebase storage

  • angular
  • firebase storage
  • drag and drop
  • file upload
  • angular directive

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:01

    in this episode we're going to build a

  2. 0:03

    drag and drop file upload feature that

  3. 0:05

    uploads the file to Firebase

  4. 0:08

    storage there's already several file

  5. 0:11

    upload packages for angular 4 and

  6. 0:12

    angular 2 such as Drop Zone JS and

  7. 0:15

    others but none of them tie in easily

  8. 0:18

    with Firebase storage and it's

  9. 0:19

    relatively straightforward to do the

  10. 0:21

    entire thing from scratch which I'll

  11. 0:23

    show you how to do in this

  12. 0:25

    lesson let's get started by building the

  13. 0:28

    service I have another lesson that goes

  14. 0:30

    through Firebase file uploads in more

  15. 0:33

    detail so for now I'm just going to run

  16. 0:35

    through the bare minimum code that's

  17. 0:36

    needed to get angular working with

  18. 0:38

    Firebase

  19. 0:39

    storage first we Define an upload class

  20. 0:42

    that models the data we want to send to

  21. 0:44

    the

  22. 0:45

    database in this example we build the

  23. 0:48

    object by sending it a Javascript file

  24. 0:50

    object to the

  25. 0:53

    Constructor in the upload service we

  26. 0:56

    import this class as well as the angular

  27. 0:58

    Fire 2 package

  28. 1:00

    and we also need to import the entire

  29. 1:02

    Firebase SDK because file uploads aren't

  30. 1:05

    supported at this point by the angular

  31. 1:07

    Fire 2

  32. 1:09

    package now we create a function to

  33. 1:11

    handle the upload task it's going to use

  34. 1:14

    the Firebase API to create a promise

  35. 1:17

    which we can use to monitor the status

  36. 1:19

    of the upload

  37. 1:21

    itself when the upload task is complete

  38. 1:24

    it'll save some information about the

  39. 1:25

    file to the real-time database most

  40. 1:27

    importantly the download URL which we

  41. 1:29

    can use to access the

  42. 1:31

    [Music]

  43. 1:36

    file now we can start building the

  44. 1:38

    attribute directive in this case we go

  45. 1:41

    with an attribute directive over a

  46. 1:42

    component because the functionality

  47. 1:45

    itself doesn't need an actual HTML

  48. 1:48

    template inside the directive we're

  49. 1:50

    going to use event emitter post listener

  50. 1:53

    and

  51. 1:54

    output we use a combination of output

  52. 1:57

    and event emitter to create our own C