Firebase Web文件上传代码:“ app / duplicate-app”

时间:2018-10-10 22:02:40

标签: javascript jquery firebase firebase-storage

我试图通过Firebase在我的网站上上传图像和视频, 第一次上传有效,第二次上传在我的控制台上显示此消息:

  

firebase.js:1未捕获   {代码:“ app / duplicate-app”,消息:“ Firebase:名为'[DEFAULT]'的Firebase应用已存在(app / duplicate-app)。“,名称:” [DEFAULT]“,堆栈:” [DEFAULT] :Firebase:名为'[[DEFAULT] ...(https://code.jquery.com/jquery-3.3.1.js:4991:28)“}的Firebase应用   代码:“ app / duplicate-app”   消息:“ Firebase:名为'[DEFAULT]'的Firebase应用程序已经存在(app / duplicate-app)”。   名称:“ [DEFAULT]”   堆栈:“ [DEFAULT]:Firebase:名为'[DEFAULT]'的Firebase应用已存在(app / duplicate-app)。↵错误(https://www.gstatic.com/firebasejs/5.5.3/firebase.js:1:49452)↵Object.initializeApp(https://www.gstatic.com/firebasejs/5.5.3/firebase.js:1:48095)↵在HTMLAnchorElement的fileInput(http://xxx.tv/theme/js/js.js:115:12)↵。在HTMLAnchorElement.elemData.handle(http://xxx.tv/theme/js/js.js:93:13)的HTMLAnchorElement.dispatch(https://code.jquery.com/jquery-3.3.1.js:5183:27)}   原始:错误

当我尝试上传视频时也无法正常工作时,它还会返回一条消息,提示我没有发送任何数据:

  

未捕获的e {code_:“存储/无效参数”,message _:“ Firebase存储:put中索引0处的无效参数:预期的Blob或文件。”,serverResponse_:null,name _:“ FirebaseError” }

HTML:

    <label for="videoUpload">
        <i for="videoUpload" class="material-icons videoUploadIcon">videocam</i>
        <input id="videoUpload" class="videoUpload" type="file" accept="video/*" style="display: none;">
    </label>

JS文件

 $(document).ready(function(){
   var config = {
    apiKey: "",
    authDomain: "web-test-.com",
    databaseURL: "com",
    projectId: ",
    storageBucket: ".appspot.com",
    messagingSenderId: ""
  };
    firebase.initializeApp(config);
});

    $(function(){
        $("#postBtn").on('click', function(e){
            e.preventDefault();
            var postField = $('#postField').val();
            var image = $('#imgupload')[0].files[0];
            var video = $('.videoUpload')[0].files[0];
            fileInput(postField,image,video);
                $('#contentToUpload').hide();
                $('.videoUploadIcon').show();       
        });
    });
// <!-- ADD POST -->
// INPUT FILE FUNCTION
function fileInput(postField, image, video) {

    if (image != null) {
        uploadFile(image)
    } else if (video != null) {
        uploadFile(video)
    } else {
        console.log('youre upload cant be blank!');
    }


}


function uploadFile(file) {
var database = firebase.database();
var storage = firebase.storage();
const storageRef = storage.ref();
    var generatedName = Math.random().toString(36).substr(2, 20);
    const uploadTask = storageRef.child(`images/${generatedName}`).put(file); //create a child directory called images, and place the file inside this directory
    uploadTask.on('state_changed', (snapshot) => {
        uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, function(snapshot) {
            var percent = snapshot.bytesTransferred / snapshot.totalBytes * 100;
            $('.progress').css("display", "block");
            $('.numPercentage').css("display", "block");
            $('.determinate').width(parseInt(percent) + '%');
            $('.numPercentage').html(parseInt(percent) + '%');
        });
    }, (error) => {
        // Handle unsuccessful uploads
        console.log(error);
    }, () => {
        uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
            console.log('File available at', downloadURL);
        });
        console.log('success');
        setTimeout(function() {
            $('.numPercentage').fadeOut(1000).css("display", "none");
            $('.determinate').fadeOut(1000).css("display", "none");
            $('.progress').fadeOut(1000).css("display", "none");
            $('.numPercentage').val("");
            $('.determinate').val("");
        }, 2000);
        document.getElementById("postField").value = null;
        document.getElementById("imgupload").value = null;
        document.getElementById("videoUpload").value = null;
        setTimeout(function() {
            $('.feed-posts').load('/php/posts.php').fadeIn('slow');
        }, 1000);
    });
}
}

// INPUT FILE FUNCTION

1 个答案:

答案 0 :(得分:1)

每个页面加载一次只能调用firebase.initializeApp()。现在,您每次上载时都会调用它。