简体   繁体   English

允许用户从谷歌云存储下载单个和批量文件

[英]Alllow users to download single & bulk files from google cloud storage

I have a react / node JS program with a material UI data table displaying rows from a mongoDB storage.我有一个 react / node JS 程序,其中包含一个材料 UI 数据表,显示来自 mongoDB 存储的行。

I would like users to be able to either download a single row, or, select multiple rows and then download all the files related to these rows.我希望用户能够下载单行,或者 select 多行,然后下载与这些行相关的所有文件。

The rows contain the location of the files in google cloud storage.这些行包含文件在谷歌云存储中的位置。

I tried to download the files in the react side using the following code:我尝试使用以下代码在反应端下载文件:

const downloadFiles = () => {
    const pathReference = ref(storage, `${cellInfo.fileName}`);
    getDownloadURL(ref(storage, `${cellInfo.fileName}`))
      .then((pathReference) => {
        // `url` is the download URL for 'images/stars.jpg'

        // This can be downloaded directly:
        const xhr = new XMLHttpRequest();
        xhr.responseType = 'blob';
        xhr.onload = (event) => {
          const blob = xhr.response;
        };
        console.log(pathReference);
        xhr.open('GET', pathReference);
        xhr.send();
      })
      .catch((error) => {
        // Handle any errors
      });
  };

However this does not work at all.然而,这根本不起作用。 I set the cors policy as defined in the firebase storage tutorial.我设置了 firebase 存储教程中定义的 cors 策略。 The path reference I get provided is valid, and, when I type it into my web browser it downloads the file, however this doesnt work when calling the function from my react app.我提供的路径参考是有效的,当我在我的 web 浏览器中输入它时,它会下载文件,但是从我的 react 应用程序调用 function 时这不起作用。 Is this anything to do with me hosting it on local host?这与我将其托管在本地主机上有关吗?

  const downloadFiles = async () => {
    await axios
      .get(`http://localhost:3000/api/${location}/download/${cellInfo._id}`)
      .then((res) => {
        console.log(res);
      })
      .catch((err) => {
        console.log(err);
      });
  };

I tried this to link to the Node JS back end however still couldn't find a valid solution.我尝试将其链接到 Node JS 后端,但仍然找不到有效的解决方案。

Update:更新:

I managed to download single files using the below method (credit to this post )我设法使用以下方法下载了单个文件(归功于这篇文章

 const downloadFiles = () => {
    const pathReference = ref(storage, `${cellInfo.fileName}`);
    getDownloadURL(ref(storage, `${cellInfo.fileName}`))
      .then((pathReference) => {
        // `url` is the download URL for 'images/stars.jpg'

        // This can be downloaded directly:
        const xhr = new XMLHttpRequest();
        xhr.responseType = 'blob';
        xhr.onload = (event) => {
          const blob = xhr.response;
          const link = document.createElement('a');
          link.href = pathReference;
             
          // Append to html link element page
          document.body.appendChild(link);

          // Start download
          link.click();

          // Clean up and remove the link
          link.parentNode.removeChild(link);
        };
        console.log(pathReference);
        xhr.open('GET', pathReference);
        xhr.send();
      })
      .catch((error) => {
        // Handle any errors
      });
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM