swg*_*r14 5 firebase firebase-storage
使用SDK的WEB版本将映像存储到Firebase Storage。文件确实已上传,但在尝试获取下载URL时始终收到以下消息
code:"storage/object-not-found"
message:"Firebase Storage: Object 'rainbow_photos/daniel.jpg' does not exist."
name:"FirebaseError"
serverResponse:"{? "error": {? "code": 404,? "message": "Not Found. Could not get object"? }?}"
Run Code Online (Sandbox Code Playgroud)
但是文件daniel.jpg确实存储在rainbow_photos文件夹中。
这是我们放置文件的方式:
rainbowPhotoUploader.addEventListener('change', function(e){
//Get file
var file = e.target.files[0];
//Create a storage ref
var storageRef = firebase.storage().ref('rainbow_photos/' + file.name);
//Upload file
storageRef.put(file);
//Get URL and store to pass
storageRef.getDownloadURL().then(function(result){
$('#rainbowPhotoURL').val(result);
});
});
Run Code Online (Sandbox Code Playgroud)
上传后立即获得下载链接,但尚未完成。
执行此操作以在上传完成后获取链接:
rainbowPhotoUploader.addEventListener('change', function(e){
//Get file
var file = e.target.files[0];
//Create a storage ref
var storageRef = firebase.storage().ref('rainbow_photos/' + file.name);
//Upload file
storageRef.put(file).then(function(result){
//Get URL and store to pass
storageRef.getDownloadURL().then(function(result){
$('#rainbowPhotoURL').val(result);
});
});
});
Run Code Online (Sandbox Code Playgroud)
基本上是 Austin 所说的,除了我们很聪明(我们很聪明,相信我!)并且我们将在上传后返回 Promise 中的下载 URL,因此您不必进行第二次获取:
rainbowPhotoUploader.addEventListener('change', function(e){
//Get file
var file = e.target.files[0];
//Create a storage ref
var storageRef = firebase.storage().ref('rainbow_photos/' + file.name);
//Upload file
storageRef.put(file).then(function(snapshot){
$('#rainbowPhotoURL').val(snapshot.downloadURL);
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4591 次 |
| 最近记录: |