将图像从 URL 上传到 Firebase 存储 [英] Upload image from URL to Firebase Storage
问题描述
我想知道如何通过 URL 而不是输入将文件上传到 Firebase 的存储(例如).我正在从网站上抓取图像并检索它们的 URL.我想通过 foreach 语句传递这些 URL 并将它们上传到 Firebase 的存储.现在,我让 firebase upload-via-input 使用以下代码:
I'm wondering how to upload file onto Firebase's storage via URL instead of input (for example). I'm scrapping images from a website and retrieving their URLS. I want to pass those URLS through a foreach statement and upload them to Firebase's storage. Right now, I have the firebase upload-via-input working with this code:
var auth = firebase.auth();
var storageRef = firebase.storage().ref();
function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
var file = evt.target.files[0];
var metadata = {
'contentType': file.type
};
// Push to child path.
var uploadTask = storageRef.child('images/' + file.name).put(file, metadata);
// Listen for errors and completion of the upload.
// [START oncomplete]
uploadTask.on('state_changed', null, function(error) {
// [START onfailure]
console.error('Upload failed:', error);
// [END onfailure]
}, function() {
console.log('Uploaded',uploadTask.snapshot.totalBytes,'bytes.');
console.log(uploadTask.snapshot.metadata);
var url = uploadTask.snapshot.metadata.downloadURLs[0];
console.log('File available at', url);
// [START_EXCLUDE]
document.getElementById('linkbox').innerHTML = '<a href="' + url + '">Click For File</a>';}
问我用什么代替
var 文件 = evt.target.files[0];
var file = evt.target.files[0];
使用外部 URL 而不是手动上传过程?
with to make it work with external URL instead of a manual upload process?
var file = "http://i.imgur.com/eECefMJ.jpg";不起作用!
var file = "http://i.imgur.com/eECefMJ.jpg"; doesn't work!
推荐答案
如果您所做的只是保存 url 路径,则无需使用 Firebase Storage.Firebase 存储用于存储物理文件,而 Firebase 实时数据库可用于存储结构化数据.
There's no need to use Firebase Storage if all you're doing is saving a url path. Firebase Storage is for physical files, while the Firebase Realtime Database could be used for structured data.
示例.一旦您从外部网站获得图片网址,这就是您所需要的:
var externalImageUrl = 'https://foo.com/images/image.png';
然后将其存储在 json 结构化数据库中:
then you would store this in your json structured database:
databaseReference.child('whatever').set(externalImageUrl);
或
如果您想将物理图像直接从外部站点直接下载到存储,那么这将需要发出 http 请求并接收 blob 响应,或者可能需要服务器端语言..
If you want to actually download the physical images straight from external site to storage then this will require making an http request and receiving a blob response or probably may require a server side language ..
Javascript 解决方案:如何使用javascript从url保存文件
Javascript Solution : How to save a file from a url with javascript
PHP 解决方案:从 PHP URL 保存图片
这篇关于将图像从 URL 上传到 Firebase 存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!