如何将mp3 blob上传到firebase存储

时间:2016-10-23 15:55:42

标签: cordova ionic2 firebase-storage

每次尝试将大量音频数据上传到firebase存储时,我都会收到404个未知的FirebaseError。但当 我单击使用.put(blob)生成的链接,我可以看到生成的JSON。当从相机插件上传图像数据时,我不会收到此错误。

错误: 无法加载资源:服务器响应状态为404(未找到)

{“code”:“storage / unknown”,“message”:“Firebase存储:发生未知错误,请检查服务器响应的错误有效负载。”,“serverResponse”:“”,“name”:“ FirebaseError“}

链接:https://firebasestorage.googleapis.com/v0/b/copout-54f4e.appspot.com/o?name=record%2Fcopoutrecording.mp3

--------------------------------

系统信息:

Cordova CLI:6.3.1
离子框架版本:2.0.0-rc.0-201610131811
离子CLI版本:2.1.1
Ionic App Lib版本:2.1.1
离子应用程序脚本版本:0.0.36
OS:
节点版本:v4.6.0

代码: 的index.html

 <ion-app></ion-app>
    <!--fire base data services-->
    <script src="js/firebase.js"></script>
 <!--<script src="https://www.gstatic.com/firebasejs/3.5.0/firebase.js"></script>-->
<script>
  // Initialize Firebase
  var config = {
     apiKey: "key",
    authDomain: "domain",
    databaseURL: "url",
    storageBucket: "storage",
    messagingSenderId: "id"
  };
  firebase.initializeApp(config);
</script>

recordpage.ts

import { Injectable } from '@angular/core';
import { MediaPlugin } from 'ionic-native';
import {FireBaseData} from './firebase.service';
import { File } from 'ionic-native';

declare var cordova: any;
// firebase
declare var firebase: any;

export enum AudioRecorderState {
Ready,
Recording,
Recorded,
Playing
}

@Injectable()
export class AudioRecorder {
mediaPlugin: MediaPlugin = null;
state: AudioRecorderState = AudioRecorderState.Ready;
mdeastr: string; 
fbd: any;
progress: string; 
file: any;

constructor() {
this.fbd = new FireBaseData();
}

get MediaPlugin(): MediaPlugin {
if (this.mediaPlugin == null) {
this.mediaPlugin = new MediaPlugin("copoutrecording.mp3", null);
}

return this.mediaPlugin;
}

upload() {
var test = JSON.stringify(this.MediaPlugin);

this.file = { name: "copoutrecording.mp3" };

   File.readAsDataURL(cordova.file.externalRootDirectory, this.file.name).then((data: any) => {
   if (data) {
   var blob = new Blob([data], { type: "audio/mp3" }); 
   this.fbd.uploadrecording(blob);
  }
});
}

firebase.service.ts

import { Injectable } from '@angular/core';
import { Platform } from 'ionic-angular';
import { File } from 'ionic-native';

declare var firebase: any;
declare var cordova: any;

@Injectable()
export class FireBaseData {

file: any;
recordstorage: any;

constructor() {
var rec = firebase.storage();
var recref = rec.ref();
this.recordstorage = recref.child("record");
}

public uploadrecording(blob: any) {

var uploadTask = this.recordstorage.child('copoutrecording.mp3').put(blob);

// Listen for state changes, errors, and completion of the upload.
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
  function (snapshot) {

    // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
    var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
    console.log('Upload is ' + progress + '% done' + ": " + snapshot.bytesTransferred + " / " + snapshot.totalBytes);

    switch (snapshot.state) {
      case firebase.storage.TaskState.PAUSED: // or 'paused'
        console.log('Upload is paused');
        break;
      case firebase.storage.TaskState.RUNNING: // or 'running'
        console.log('Upload is running');
        //     alert("media uploading to firebase");
        break;
    }
  }, function (error) {
    switch (error.code) {
      case 'storage/unauthorized':
      // User doesn't have permission to access the object
        console.log(JSON.stringify(error));
        break;

      case 'storage/canceled':
        console.log(JSON.stringify(error));
        //  alert("cancelled: " + error.serverResponse);
        // User canceled the upload
        break;


      case 'storage/unknown':
        console.log(JSON.stringify(error));
        //    alert("error: " + error.message);
        //      alert("error: " + JSON.stringify(error));
        // Unknown error occurred, inspect error.serverResponse
        break;
    }
  }, function () {

    // Upload completed successfully, now we can get the download URL
    var downloadURL = uploadTask.snapshot.downloadURL;
    alert("made it to firebase" + downloadURL);
  });
}

}

0 个答案:

没有答案