【问题标题】:How to ask for permission to download files on NextJS?如何请求在 NextJS 上下载文件的权限?
【发布时间】:2022-02-02 03:33:43
【问题描述】:

我有以下端点:

baseurl.com/api/v1/datasheet/format

访问此网址将根据数据表下载特定信息。例如,在 Express 中,如果 format = json,我会返回此方法:

const generateJSON = (res, data) => {
    res.header('Content-Type', 'application/json');
    res.attachment('example.json');
    return res.send(data);
}

这工作正常,但它会自动将文件下载为“example.json”,而无需询问用户是否允许下载文件

我的目的是在我的前端有一个按钮(使用 NextJS),它会询问用户是否要下载文件(经典下载或取消下载提示),然后打开文件资源管理器等。

我该怎么做?这是来自后端还是前端?

【问题讨论】:

    标签: node.js reactjs express download next.js


    【解决方案1】:

    使用Content-disposition: attachment; filename=example.json 触发另存为对话框。

    const generateJSON = (res, data) => {
        res.header('Content-Type', 'application/json');
        res.header('Content-disposition', 'attachment');
        res.attachment('example.json');
        return res.send(data);
    }
    
    
    

    HTTP 上下文中的第一个参数是 inline(默认值,表示它可以显示在网页内,或作为网页)或附件(表示应该下载它;大多数浏览器显示“另存为”对话框,预填充文件名参数的值(如果存在)。

    通过https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#:~:text=The%20first%20parameter,parameters%20if%20present)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-30
      • 2018-05-31
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      相关资源
      最近更新 更多