The upload class will be used in the service layer. Notice it has a constructor for file attribute, which has a type of File. This will allows us to initialize new uploads with a JavaScript File object. You will see why this is important in the next step.

export class Upload {
$key: string;
file:File;
name:string;
url:string;
progress:number;
createdAt: Date = new Date();
constructor(file:File) {
this.file = file;
}
}

Then build the upload service, which can inject to component:

import { Injectable } from '@angular/core';
import {Subject} from 'rxjs/Subject';
import {MatSnackBar} from '@angular/material'; import * as firebase from 'firebase';
import UploadTaskSnapshot = firebase.storage.UploadTaskSnapshot;
import {Upload} from './upload'; @Injectable()
export class UploadService { uploading$ = new Subject<number>();
completed$ = new Subject<Upload>(); constructor(
private snackBar: MatSnackBar
) { } uploadFile(upload: Upload, folder: string) { // Create a storage ref
const storageRef = firebase.storage().ref();
const uploadTask = storageRef.child(`${folder}/${upload.file.name}`).put(upload.file);
// Upload file
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
(snapshot: UploadTaskSnapshot) => {
upload.progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
this.uploading$.next(upload.progress);
},
(err) => {
this.snackBar.open(err.message, 'OK', {
duration: 3000,
});
},
() => {
upload.url = uploadTask.snapshot.downloadURL;
upload.name = upload.file.name;
this.completed$.next(upload);
this.uploading$.next(null);
});
} deleteUpload(name: string, folder: string) {
const storageRef = firebase.storage().ref();
storageRef.child(`${folder}/${name}`).delete();
this.completed$.next();
}
}

Component:

import {ChangeDetectionStrategy, Component, forwardRef, Input} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms'; import {UploadService} from '../../services/upload.service';
import {Upload} from '../../services/upload';
import {Observable} from 'rxjs/Observable'; export const TYPE_CONTROL_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
multi: true,
useExisting: forwardRef(() => ImageUploaderComponent)
}; @Component({
selector: 'image-uploader',
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [TYPE_CONTROL_ACCESSOR],
templateUrl: './image-uploader.component.html',
styleUrls: ['./image-uploader.component.scss']
})
export class ImageUploaderComponent implements ControlValueAccessor { @Input() img; private onTouch: Function;
private onModelChange: Function;
private value: string; file: Upload;
currentUpload: Upload;
progress$: Observable<number>; constructor(private uploadService: UploadService) {
this.progress$ = this.uploadService.uploading$;
this.uploadService.completed$.subscribe((upload) => { if (upload) {
this.setSelected(upload.url);
this.currentUpload = upload;
} else {
this.setSelected('');
this.currentUpload = null;
}
});
} onChange($event) {
const file = $event.target.files[0];
this.file = new Upload(file);
this.uploadService.uploadFile(this.file, 'icons');
} writeValue(value: any): void {
this.value = value;
} registerOnChange(fn: Function): void {
this.onModelChange = fn;
} registerOnTouched(fn: Function): void {
this.onTouch = fn;
} setSelected(value: string): void {
this.value = value;
this.onModelChange(value);
this.onTouch();
} clear() {
if (this.file) {
this.uploadService.deleteUpload(this.file.name, 'icons');
this.setSelected('');
}
}
}

Template:

    <div *ngIf="progress$ | async as p">
<mat-progress-bar mode="determinate" [value]="p"></mat-progress-bar>
</div>
<mat-card-subtitle>
Select / upload icon
</mat-card-subtitle> <mat-card-content fxLayout="column">
<div fxLayout="row" fxLayoutAlign="space-around">
<div
*ngIf="currentUpload"
class="image-container"
fxFlex="30%">
<img [src]="currentUpload?.url || ''" [alt]="currentUpload?.name || ''">
</div>
</div>

[AngularFire] Angular File Uploads to Firebase Storage with Angular control value accessor的更多相关文章

  1. [Angular] Implement a custom form component by using control value accessor

    We have a form component: <label> <h3>Type</h3> <workout-type formControlName=& ...

  2. [转]File uploads in ASP.NET Core

    本文转自:https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads By Steve Smith ASP.NET MVC ...

  3. Asp.net mvc 3 file uploads using the fileapi

    Asp.net mvc 3 file uploads using the fileapi I was recently given the task of adding upload progress ...

  4. The lesser known pitfalls of allowing file uploads on your website

    These days a lot of websites allow users to upload files, but many don’t know about the unknown pitf ...

  5. 从Java角度理解Angular之入门篇:npm, yarn, Angular CLI

    本系列从Java程序员的角度,带大家理解前端Angular框架. 本文重点介绍Angular的开发.编译工具:npm, yarn, Angular CLI,它们就像Java在中的Maven,同时顺便介 ...

  6. (转载)从Java角度理解Angular之入门篇:npm, yarn, Angular CLI

    本系列从Java程序员的角度,带大家理解前端Angular框架. 本文是入门篇.笔者认为亲自动手写代码做实验,是最有效最扎实的学习途径,而搭建开发环境是学习一门新技术最需要先学会的技能,是入门的前提. ...

  7. Angular 个人深究(一)【Angular中的Typescript 装饰器】

    Angular 个人深究[Angular中的Typescript 装饰器] 最近进入一个新的前端项目,为了能够更好地了解Angular框架,想到要研究底层代码. 注:本人前端小白一枚,文章旨在记录自己 ...

  8. angular.module()创建、获取、注册angular中的模块

    // 传递参数不止一个,代表新建模块;空数组代表该模块不依赖其他模块 var createModule = angular.module("myModule", []); // 只 ...

  9. Angular入门,开发环境搭建,使用Angular CLI创建你的第一个Angular项目

    前言: 最近一直在使用阿里的NG-ZORRO(Angular组件库)开发公司后端的管理系统,写了一段时间的Angular以后发现对于我们.NET后端开发而言真是非常的友善.因此这篇文章主要是对这段时间 ...

随机推荐

  1. websocket调试工具

    http://www.blue-zero.com/WebSocket/ wss://yy.xxx.com/video/websocket/client.ws

  2. WHU 1540 Fibonacci 递推

    武大邀请赛的网络预选赛,就去做了个签到题,居然连这个递推都没推出来,真是惭愧. 而且好久没写矩阵乘法了,来回顾一下. 题意: 求Fibonacci数列的,前n项立方和. 思路: 可以求得一下递推公式: ...

  3. unity调用Android的两种方式:其二,调用aar包

    上一篇我们讲了unity如何调用jar包 http://www.cnblogs.com/Jason-c/p/6743224.html, 现在我们介绍一下怎么生成aar包和unity怎么调用aar 一. ...

  4. 【转载】GitHub详细教程

    1 Git详细教程   1.1 Git简介   1.1.1 Git是何方神圣?   Git是用C语言开发的分布版本控制系统.版本控制系统可以保留一个文件集合的历史记录,并能回滚文件集合到另一个状态(历 ...

  5. Windows cannot find ". Make sure you typed the name correctly, and then try again

    https://answers.microsoft.com/en-us/windows/forum/windows_10-desktop/windows-10-explorerexe-error-wi ...

  6. 如何判断自己IP是内网IP还是外网IP

    tcp/ip协议中,专门保留了三个IP地址区域作为私有地址,其地址范围如下: 10.0.0.0/8:10.0.0.0-10.255.255.255  172.16.0.0/12:172.16.0.0- ...

  7. centos7 zabbix3.4.6显示中文乱码问题

    工具 : winscp (Linux Windows 传输文件工具) 当部署完zabbix然后显示中文会出现如下图 然后此时先去windows的文字目录如 C:\Windows\Fonts 随便托个字 ...

  8. Announcing Zuul: Edge Service in the Cloud--转

    原文地址:http://techblog.netflix.com/2013/06/announcing-zuul-edge-service-in-cloud.html   The Netflix st ...

  9. SCOPE_IDENTITY()和 SELECT @@IDENTITY 的用法

    这俩个,是在插入数据的时候使用,返回刚刚插入数据行的ID 大家慎用@@IDENTITY,而尽量采用 SCOPE_IDENTITY() 函数替换之. SCOPE_IDENTITY()  也是得到最后一条 ...

  10. UVA Watering Grass

    贪心算法. #include <iostream> #include <cstdio> #include <cstring> #include <queue& ...