Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Javascript将图像副本(位于URL)保存到Firebase存储(web)中?_Javascript_Firebase_Firebase Storage - Fatal编程技术网

如何使用Javascript将图像副本(位于URL)保存到Firebase存储(web)中?

如何使用Javascript将图像副本(位于URL)保存到Firebase存储(web)中?,javascript,firebase,firebase-storage,Javascript,Firebase,Firebase Storage,我正在尝试创建一个图像的副本(位于url上),并将其保存到Firebase的存储设施中。我想存储实际的图像文件,而不仅仅是url。如果我理解正确,我首先需要将url上的图像转换为blob或文件,然后将数据上载到Firebase存储 这是我当前使用Javascript的尝试: function savePicture(){ var url = ["http://carltonvet.com.au/sites/default/files/styles/large/public/images/

我正在尝试创建一个图像的副本(位于url上),并将其保存到Firebase的存储设施中。我想存储实际的图像文件,而不仅仅是url。如果我理解正确,我首先需要将url上的图像转换为blob或文件,然后将数据上载到Firebase存储

这是我当前使用Javascript的尝试:

function savePicture(){
    var url = ["http://carltonvet.com.au/sites/default/files/styles/large/public/images/article/cats.jpg"];
    var blobpic = new Blob(url, {type: 'file'}); 
    console.log(blobpic);

    var user = firebase.auth().currentUser;
        if (user != null) {
            var userid = user.uid; 

            var ref = firebase.storage().ref(userid + "profilePhoto");
            ref.put(blobpic).then(function(snapshot) {
                console.log('Picture is uploaded!');
                console.log(snapshot);

                var filePath = snapshot.metadata.fullPath;
                document.getElementById('picTestAddress').innerHTML = ""+filePath;
                document.getElementById('picTestImage').src = ""+filePath;
        });
        }else{
                console.log("Something went wrong, user is null.");
        }
}
我有两个HTML标记,如下所示:

<div id="picTestAddress"></div>
<img id="picTestImage" />

)

为了找到正确的路径,我在Firebase控制台中导航到该文件的位置,并复制了“下载url”地址-但当我选中该地址时(通过将其输入我的web浏览器),它加载了一个白色页面,其中包含一行文本,即原始url:“

现在我想我只是把url保存到了存储器中,而不是实际的图像文件

我一直在关注Firebase文档,我认为上传部分工作正常,但我认为在使用Javascript将url转换为blob/文件时失败了

我已经看了一些其他的问题,比如这个:我打算在这里学习这个例子:但是它说它现在是一个遗留的例子,我似乎在Firebase的示例部分找不到更新的版本

任何关于如何做到这一点的建议或帮助都将不胜感激。 提前谢谢你

以下工作:

function savePhoto(){

    var url = "http://www.planetware.com/photos-large/F/france-paris-eiffel-tower.jpg";
    // First, download the file:
    var xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';
    xhr.onload = function(event) {
    var blob = xhr.response;

    // Get the current user:            
    var user = firebase.auth().currentUser;
    if (user != null) {
    var userid = user.uid; 

    // Define where to store the picture:
    var picRef = firebase.storage().ref(userid + "/profilePhoto");

    // Store the picture:
    picRef.put(blob).then(function(snapshot) {
    console.log('Picture uploaded!');

    // Now get image from storage and display in div...
    picRef.getDownloadURL().then(function(pic) {
        var userspic = pic;
        document.getElementById('picTestImage').src = userspic;

    }).catch(function(error) {
        console.log("There was an error: "+error);
    });

    });
    }else{
        console.log("We weren't able to confirm there is a current user, something went wrong.");
    }

  };
  xhr.open('GET', url);
  xhr.send();
}

看起来不错,不过我也会考虑一个被允诺的版本:

function getBlob(url) {
  return new Promise(resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';
    xhr.onload = function(event){
      var blob = xhr.response;
      resolve(blob);
    };
    xhr.onerror = reject();
    xhr.open('GET', url);
    xhr.send();
  }
}

function storageURLForPhoto(oldURL, newName) {
  getBlob(oldURL)
  .then(function(blob) {
    var picRef = firebase.storage().ref().child(newName);
    return picRef.put(blob)
  })
  .then(function(snapshot) {
    return snapshot.downloadURL;
  });
  .catch(function() {
    // handle any errors
  })
}

更容易推理:)

我想第一个问题是:为什么?您可以将另一个URL存储在实时数据库中,并轻松地将其下载到您的应用程序中(请参见Zero To app:)。否则,你需要下载XHR文件,然后重新上传,就像这样:嗨,迈克,谢谢你的评论,我想存储图像而不是url,以防托管图像的第三方更改或删除它,这反过来会破坏我应用程序上的链接。您发布的链接帮助我使其正常工作,我将在下面发布解决方案。再次感谢!