将图片从URL上传到Firebase存储 [英] Upload image from URL to Firebase Storage

查看:70
本文介绍了将图片从URL上传到Firebase存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何通过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 file = evt.target.files [0];

var file = evt.target.files[0];

with使其与外部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);

OR

如果您想直接从外部站点直接下载物理映像到存储,那么这将需要发出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 Solution : How to save a file from a url with javascript

PHP解决方案:从PHP URL保存图像

这篇关于将图片从URL上传到Firebase存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆