twbs / ratchet

Build mobile apps with simple HTML, CSS, and JavaScript components.

Home Page:http://goratchet.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

readAsArraybuffer issue

kaouterrhazlani opened this issue · comments

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 };