小编fey*_*diz的帖子

如何从前端下载带有授权标头的文件

这是我的后端代码。它有 2 条受保护的路线。需要授权标头(承载令牌)。

  • 这些路线允许您下载您的照片和简历。

我的后端没有任何问题。它正在与邮递员和移动应用程序配合使用。我认为前端有一个限制。

我不想

  • 我不想使用 fetch 将这些文件下载为 blob。因为它不允许浏览器显示进度条。
  • 我不想将身份验证方法从 Bearer token 更改为 cookie。我已经可以用 cookie 做到这一点了。

我想知道为什么 Bearer token 比 cookie 更受欢迎,如果我必须使用 cookie 来实现这一点。

后端

// other imports .....
const path = require('path');
const fs = require('fs');
const express = require('express');
const app = express();

app.use((req, res, next) => {
  try {
    const token = req.get('Authorization');
    if(!token) {
      throw new Error('401');
    }
    const userId = getIdFromToken(token);
    if(!userId) {
      throw new Error('401');
    }
    res.locals.userId = userId;
    next();
  } catch (error) …
Run Code Online (Sandbox Code Playgroud)

authentication cookies download express bearer-token

6
推荐指数
1
解决办法
3949
查看次数