如何从 Vue Web 应用程序中的 firebase 存储下载文件?

丹增达凯

情况:您需要通过具有 vue.js 网络应用程序(包含 vuetify)的浏览器将 pdf 文件(或任何文件类型)从 firebase 存储下载到您的计算机。您已经查看了firebase storage doc,但它不是很清楚。您不确定适合您的 Firebase 存储规则。这是用于前端 - 用户的浏览器。你将如何实施?

丹增达凯

这是关于如何通过 vue 网络应用程序将文件从 firebase 存储下载到用户计算机的编译答案。火力存储下载的文件有90%的您需要为您的vue.js web应用程序下载文件到您的计算机。按照那里的步骤操作。如果您没有使用 firebase 身份验证,那么您可能希望为所有人设置读取访问权限。默认情况下,只有经过身份验证的用户才能读写。您可以在 Firebase 存储规则中这样做:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read: if request.auth == null;
      allow write: if request.auth != null;
    }
  }
}

这允许任何人查看/下载您的文件。接下来,按照上面发布的指南,您将能够获取文件的 url,然后将其转换为“blob”格式。现在,该指南没有告诉您接下来要做什么,但另一个Stack Overflow 答案有解决方案。我在这里结合了两者。基本上代码中发生的事情是:

  1. 您应该从@click.prevent事件发射器而不是@click.
  2. 事件处理函数做了几件事。与 firebase 存储文档一样,它获取文件的 url 并通过 XMLHttpRequest() 将其转换为“blob”。
  3. 按照链接的解决方案,您应该创建一个新a元素,然后将其href属性分配给“blob”、下载名称并发出click事件。之后撤消该href属性以避免发生意外。

传统上,我们使用a链接来下载文件,但我们可以在事件处理程序v-btn中创建a链接时使用。我的按钮有一个工具提示和图标。另外,由于 linter,我删除了 unuses 变量。


内部 html 模板:

<v-tooltip top>
  <template v-slot:activator="{ on, attrs }">
    <v-btn
      :color="counterThemeColorClass"
      fab
      ripple
      v-bind="attrs"
      v-on="on"
      @click.prevent="downloadResumePdf"
    >
    <v-icon
      :color="themeColorClass"
      x-large
    >mdi-file-account-outline</v-icon>
    </v-btn>
  </template>
  <span class="font-weight-bold">download resume</span>
</v-tooltip>

内部脚本:

downloadResumePdf() {
  const resumeRef = firebase.storage()
    .ref('tenzin_resume.pdf');
  resumeRef.getDownloadURL().then((url) => {
    // `url` is the download URL
    console.log(url);
    // This can be downloaded directly:
    const xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';
    xhr.onload = function () {
      const blob = xhr.response;
      const link = document.createElement('a');
      link.href = URL.createObjectURL(blob);
      link.download = 'tenzin_resume';
      link.click();
      URL.revokeObjectURL(link.href);
    };
    xhr.open('GET', url);
    xhr.send();
  }).catch((error) => {
    // Handle any errors
    switch (error.code) {
      case 'storage/object-not-found':
        // File doesn't exist
        break;

      case 'storage/unauthorized':
        // User doesn't have permission to access the object
        break;

      case 'storage/canceled':
        // User canceled the upload
        break;

      case 'storage/unknown':
        // Unknown error occurred, inspect the server response
        break;
      default:
        break;
    }
  });
},

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Firebase存储下载文件功能不起作用(JavaScript / Web应用程序)

如何从Firebase存储下载文件

如何让用户从 web 应用程序上的 firebase 存储下载图像?

如何在 Firebase Flutter 应用程序中存储会话

如何在Web应用程序中存储选项列表

如何在Web应用程序中读取属性文件?

如何在Web应用程序中预览EML文件?

如何在 vue js 应用程序中单击按钮下载 json 文件

从 Web 应用程序下载文件的问题 - ASP.NET Web 窗体应用程序

从FTP下载文件仅在Azure Web应用程序中失败

如何通过REST从Jhipster应用程序下载文件?

如何在Vue.js应用程序中配置Firebase CDN /缓存?

如何正确使用s3在Web应用程序中传递和存储文件?

如何在 NodeJS Web 应用程序中存储和访问大文件

如何确定网站或Web应用程序中是否使用了vue.js?

如何使用Vue和Java EE在Web应用程序中实现聊天系统?

如何在Firebase Web应用程序中插入新的子数据?

如何防止客户修改Firebase数据(在没有后端的Web应用程序中)?

在非 Vue Web 应用程序中嵌入 Vue 应用程序(或 Vue Web 组件)

Firebase Web应用程序的管理员视图:如何

如何使用 nuxt firebase 部署 Web 应用程序?

如何从OS X应用程序中的URL下载文件

如何始终在flutter应用程序中始终静态显示从Firebase存储下载的图像,单击即可打开并查看

如何修复带有 Firebase 的 Vue 应用程序中的错误:“TypeError: firebase.auth.sendPasswordResetEmail is not a function”?

如何下载Firebase存储文件

如何在我的Google Web Engine Web应用程序中包含php.ini文件?

Android:从 Firebase 存储下载文件列表

从Web应用程序下载文件时将页面的HTML代码写入文件

如何在flutter应用程序的firebase中启用持久存储?