Firebase-图片上传有时无法正常工作

时间:2017-02-15 14:12:45

标签: image firebase firebase-realtime-database firebase-storage

我有一个基于Firebase的表单。该表单从用户获取1或2个图像,具体取决于用户的付款方式。我将文本数据存储在Firebase数据库中,并将图像存储在Firebase存储中。每个用户的文本数据还包括用户选择的图像文件的名称。一切都在80%的时间都很好。

但是对于大约20%的结果,我遇到了一个奇怪的问题。我在Firebase中获取用户的文本数据,并且该数据还包含用户选择的图像文件的名称,但是当我检查Firebase存储时,我找不到图像文件。第一个疑问是代码语法中的一些错误,但它不适用于其他80%。

基本上,我想知道在什么情况下图片无法上传到Firebase?

这是我们到目前为止的代码:

// All the variables have been initialized properly.
if(selectedFile.type == "image/png" || selectedFile.type == "image/jpeg" || selectedFile.type == "image/jpg") {
    if (payProof.type == "image/png" || payProof.type == "image/jpeg" || payProof.type == "image/jpg") {
        uploadFile();
        uploadSS();
        writeUserData(name, mobile, brand, model, type, address, pincode, payMethod, imageName);
        $("#submit").hide();
        $("#status").html("<span style='font-size: 1.6em;'>Congratulations " + name + "! Your order for " + model + " has been placed.</span>");
        console.log("Data Sent.")
    } else {
        $("#status").html("");
        alert("Invalid Screenshot image format. Upload a PNG or JPEG image.");
    }
} else {
    $("#status").html("");
    alert("Invalid cover design image format. Upload a PNG or JPEG image.");
}



function writeUserData(name, mobile, brand, model, type, address, pincode, payMethod, imageName) {
// This function also obtains the "imageName" when it is called, and in each of the cases where I couldn't find the image in Firebase Storage, I had the name of the image file that the user uploaded in the "imageName" variable.
    firebase.database().ref(name + " " + mobile.substr(0,2) + Math.floor((Math.random() * 100) + 1)).set({
        name            : name,
        mobile          : mobile,
        brand           : brand,
        model           : model,
        type            : type,
        address         : address,
        pincode         : pincode,
        paymentMethod   : payMethod,
        image           : imageName,
        done            : "no"
    });
}



function uploadFile() {
    var selectedFile = $("#image")[0].files[0];
    $("#status").html("<i class='fa fa-spinner fa-spin fa-2x fa-fw'></i>  Saving...");
    var filename = selectedFile.name;
    var storage = firebase.storage();
    var storageRef = storage.ref("(" + $("#name").val() + ")" + filename);
    var metadata = {
        contentType: selectedFile.type
    };
    var uploadTask = storageRef.put(selectedFile, metadata);
}

function uploadSS() {
    var payProof = $("#pay-proof")[0].files[0];
    var payFilename = payProof.name;
    var storage = firebase.storage();
    var storageRef = storage.ref("(" + $("#name").val() + ")" + payFilename);
    var metadata = {
        contentType: payProof.type
    };
    var uploadTask = storageRef.put(payProof, metadata);
}

0 个答案:

没有答案