Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readAsArraybuffer issue #908

Open
kaouterrhazlani opened this issue Mar 8, 2021 · 0 comments
Open

readAsArraybuffer issue #908

kaouterrhazlani opened this issue Mar 8, 2021 · 0 comments

Comments

@kaouterrhazlani
Copy link

kaouterrhazlani commented Mar 8, 2021

Can you help me please with this problem?
i want to store files on firebase .
i have all files with the same size in storage firebase (16 B ),and when i open them there are no content.

`import { Component } from '@angular/core';
import { File} from '@ionic-native/file/ngx';
import { FileChooser } from '@ionic-native/file-chooser/ngx';
import { FilePath } from '@ionic-native/file-path/ngx';
import { FireBaseService } from '../services/firebase-service.service'

@component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],

})
export class HomePage {

fileforSending ;
fileUri :string;
filePath :string;
fileName :string;
fileType :string;
buffer ;
entry;

constructor(
private fireService:FireBaseService,
private fileChooser:FileChooser,
private file :File,
private pathConverter :FilePath) {
}

choose(){
this.fileChooser.open()
.then(uri => {
//getting URI of a choosen file
this.fileUri = uri;
return this.file.resolveLocalFilesystemUrl(this.fileUri);

}).then(fileEntry => {
  this.entry = fileEntry;
  this.entry.file((arg) => {
  //getting mime type of a file
  this.fileType = arg.type;})

}).then(() => {
  return this.pathConverter.resolveNativePath(this.fileUri)

}).then((filePath) => {
  //converting file URI to normal file PATH & file NAME
  this.filePath = filePath.substring(0, filePath.lastIndexOf('/'));
  this.fileName = filePath.substring(filePath.lastIndexOf('/'), filePath.length).replace("/", "");
  
}).then(async () => {
  return await this.file.readAsArrayBuffer(this.filePath, this.fileName)
}).then(async (file) => {
    //getting file 
    await this.fireService.uploadFileToStorage(file, this.fileType, this.fileName);
})
.catch(err =>  {
    console.log('error: ', err)
});

}
}
`

import { Injectable } from '@angular/core';
import {AngularFireDatabase} from "@angular/fire/database";

import {AngularFireStorage} from "@angular/fire/storage";

@Injectable({
providedIn: 'root'
})
export class FireBaseService {

constructor(private af: AngularFireStorage){}

async uploadFileToStorage(file, type, name) {
    const randomId = Math.random()
        .toString(36)
        .substring(2, 8);

    let oMyBlob = new Blob([file], {type : type})
    console.log(oMyBlob)
    const uploadTask = this.af.upload(
        `files/${new Date().getTime()}_${randomId}_${name}`,
        oMyBlob
    );

    uploadTask.then(async res => {
        console.log('file upload finished!');
    }).catch(err =>  {
        console.log('file wasnt upload. Error: ' + err);
    });
}

}

export { FireBaseService as ExportedClass };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant