Проблема с загрузкой снимка задачи - PullRequest
0 голосов
/ 01 июня 2018

У меня проблема с отображением изображения в моем проекте.Я хочу, чтобы в задаче .subscribe URL загрузки был равен изображению.Таким образом, это может отобразить это.Но когда я это делаю, появляется сообщение об ошибке:

Тип «UploadTaskSnapshot» не может быть назначен типу «string».

Вот код:

export class PostDashboardComponent implements OnInit {

  title: string;
  image: string = null;
  content: string;

  buttonText: string = "Create Post";

  uploadPercent: Observable<number>;
  downloadURL: Observable<string>;


  constructor(
    private auth: AuthService,
    private postService: PostService, 
    private storage: AngularFireStorage
  ) { }

  ngOnInit() {
  }
  uploadImage(event) {
    const file = event.target.files[0]
    const path = `posts/${file.name}`
    const fileRef = this.storage.ref(path);
    if (file.type.split('/')[0] !== 'image') {
      return alert('only image files')
    } else {
      const task = this.storage.upload(path, file)
      this.uploadPercent = task.percentageChanges();
      task.snapshotChanges().pipe(
         finalize(() => this.downloadURL = fileRef.getDownloadURL() )
      )

.subscribe (url => (this.image = url))

      console.log('Image Uploaded!');

Что я должен изменить, чтобы заставить его работать, потому что я любитель.Спасибо за вашу помощь.

1 Ответ

0 голосов
/ 03 июня 2018

Я нашел ответ, и это:

  uploadImage(event) {
  const file = event.target.files[0]
  const path = `posts/${file.name}`
  const fileRef = this.storage.ref(path);
  if (file.type.split('/')[0] !== 'image') {
  return alert('only image files')
} else {
  const task = this.storage.upload(path, file);
  const ref = this.storage.ref(path);
  this.uploadPercent = task.percentageChanges();
  console.log('Image uploaded!');
  task.snapshotChanges().pipe(
    finalize(() => {
      this.downloadURL = ref.getDownloadURL()
      this.downloadURL.subscribe(url => (this.image = url));
    })
  )
  .subscribe();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...