简体   繁体   中英

Javascript - instantiate a new filetype from a file in the assets folder

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.
在此处输入图片说明

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM