【发布时间】:2021-02-11 23:14:36
【问题描述】:
我在使用 Firebase 存储上传图片时遇到了一些问题。我尝试在 Vue 应用程序上实现它,但在将图像上传到我的存储桶时出现net::ERR_NAME_NOT_RESOLVED 错误。我尝试按照documentation 上的教程进行操作。将allow read, write的存储规则设置为true。但仍然得到同样的错误。所以我所做的就是在单个 HTML 文件上进行最低限度的尝试,就像他们在文档中的视频教程中所做的那样。
这是我的 HTML 文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Firebase Upload</title>
<style media="screen">
body {
display: flex;
min-height: 100vh;
width: 100%;
padding: 0;
margin: 0;
align-items: center;
justify-content: center;
flex-direction: column;
}
#uploader {
-webkit-appearance: none;
appearance: none;
width: 50%;
margin-bottom: 10px;
}
</style>
</head>
<body>
<progress value="0" max="100" id="uploader">0%</progress>
<input type="file" value="upload" id="fileButton">
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/8.0.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.0.0/firebase-storage.js"></script>
<script>
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
var firebaseConfig = {
apiKey: "APIKEY",
authDomain: "AUTHDOMAIN",
databaseURL: "DABASEURL",
projectId: "PROJECTID",
storageBucket: "STORAGEBUCKET",
messagingSenderId: "MESSAGEID",
appId: "APPID",
measurementId: "MEASUREMENTID"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
var uploader = document.getElementById('uploader');
var fileButton = document.getElementById('fileButton');
fileButton.addEventListener('change', function(e) {
var file = e.target.files[0];
var storageRef = firebase.storage().ref('books/' + file.name);
var task = storageRef.put(file);
task.on('state-changed',
function progress(snapshot) {
var percentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
uploader.value = percentage;
},
function error(err) {
console.log(err);
},
function complete() {
console.log('complete');
},
);
})
</script>
</body>
</html>
当然,我用虚假的配置字符串替换了我的 Firebase 配置,但基本上这就是我所拥有的。我在 vs 代码上使用实时服务器扩展来启动网站,但仍然收到相同的错误 POST https://firebasestorage.googleapis.com/v0/b/app.appspot.com/o?name=books%image.jpg net::ERR_NAME_NOT_RESOLVED。
【问题讨论】:
-
请检查这是否适合您。 stackoverflow.com/questions/37760695/… 我试过了,但我的结果没有用。
-
这件事最近也发生在我身上。还是想办法解决。尝试了其他帐户和 Firestore 服务器,但没有解决问题。
-
这不再显示在我的应用程序上。我什么都没做。
标签: javascript firebase file-upload firebase-storage