By shock-me


2018-09-14 09:47:35 8 Comments

I need to upload a photo (png) located locally on my assets folder to Firebase (DaaS). Now as stated in the docs i need to make a new Filetype for it.
enter image description here

But how would I set such a filetype for my local img? The path of the image is this:

../../../../assets/no-photo-avatar.png

Does anyone have a suggestion?

Edit

I managed to create a reference to the file with this:

const defaultPhoto: HTMLImageElement = new Image();
defaultPhoto.src = '../../../../assets/no_photo_avatar.png';

But now I need to convert it it to a new File() or new Blob() type.

1 comments

@shock-me 2018-09-14 16:10:41

To convert the image type to a filetype I did this - full solution:

  upload(): Promise<void> {
     const defaultPhoto: HTMLImageElement = new Image();
     defaultPhoto.src = '../../../../assets/no_photo_avatar.png';
     return srcToFIle(defaultPhoto.src, defaultPhoto.name, 'image/png').then() => ...) <= firebase methods
  }

  private srcToFile(src, fileName, mimeType): Promise<File> {
     return (fetch(src)
    .then(res => res.arrayBuffer())
    .then(buf => new File([buf], fileName, {type: mimeType})));

}

btw. the typings are typescript syntax.

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] How can I add new array elements at the beginning of an array in Javascript?

  • 2011-11-10 00:35:22
  • Moon
  • 560917 View
  • 1177 Score
  • 10 Answer
  • Tags:   javascript arrays

36 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

69 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 5016372 View
  • 6304 Score
  • 69 Answer
  • Tags:   javascript arrays

14 Answered Questions

[SOLVED] What is the 'new' keyword in JavaScript?

53 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

26 Answered Questions

[SOLVED] Open a URL in a new tab (and not a new window) using JavaScript

  • 2011-02-05 15:52:13
  • Mark F
  • 1951388 View
  • 1629 Score
  • 26 Answer
  • Tags:   javascript

1 Answered Questions

Access android assets folder using javascript file

0 Answered Questions

Firebase storage save files privately for app use only

3 Answered Questions

[SOLVED] How do I remove a key from a JavaScript object?

  • 2010-08-11 04:59:03
  • Martin Ongtangco
  • 618089 View
  • 904 Score
  • 3 Answer
  • Tags:   javascript

1 Answered Questions

Sponsored Content