相关疑难解决方法(0)

如何使用window.fetch下载文件?

如果我想下载文件,我应该在then下面的块中做什么?

function downloadFile(token, fileId) {
  let url = `https://www.googleapis.com/drive/v2/files/${fileId}?alt=media`;
  return fetch(url, {
    method: 'GET',
    headers: {
      'Authorization': token
    }
  }).then(...);
}
Run Code Online (Sandbox Code Playgroud)

请注意,代码位于客户端.

javascript fetch fetch-api

37
推荐指数
8
解决办法
4万
查看次数

使用经过身份验证的REST中的fetch()下载和保存数据

我有一个React应用程序,使用Python和Flask构建的REST后端.我正在从数据库下载数据并通过浏览器将其保存为CSV文件. 我有这个工作. 然而,我不明白的原因是为什么我必须超越我一直在阅读的资源并将其混合以使其发挥作用.为什么我没有发现这个概述更好?

有人说我要做的就是用mimetype设置响应头,并且Content-Disposition: attachment; filename=something.csv:

然而,仅此一点,仅使用普通链接,而不是fetch()和身份验证,所以我不得不寻找将客户端数据保存到磁盘的方法,例如:

所以,我的问题是:

  • 为什么我必须这样做?, 要么
  • 我错过了什么 - 更简单的方法是什么?

似乎第一个问题的答案是我不能修改请求头(添加身份验证令牌),除非通过XHR类型的工作.这似乎在这里得到了回答(没有回答,真的):

并且,由于某种原因,对XHR的反应Content-Disposition: attachment毫无意义.真的吗?在现代浏览器中管理这样的请求是否有更内在的方法?

我觉得我不明白这一点,这让我感到烦恼.

无论如何,这是我希望简化的工作代码,如果可能的话:

JS(React):

// https://stackoverflow.com/a/18197511/680464
download(filename, text) {
    var pom = document.createElement('a');
    pom.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(text));
    pom.setAttribute('download', filename);

    if (document.createEvent) {
        var event = document.createEvent('MouseEvents');
        event.initEvent('click', true, true);
        pom.dispatchEvent(event);
    }
    else {
        pom.click();
    }
}
downloadReport(studyID) {
    fetch(`/api/admin/studies/${studyID}/csv`
    ,   {
            headers: {
                "Authorization": "Bearer " + this.props.authAPI.getToken()
            ,   "Accept": "text/csv" …
Run Code Online (Sandbox Code Playgroud)

javascript ajax xmlhttprequest flask reactjs

9
推荐指数
1
解决办法
6220
查看次数

如何使用Fetch API下载和保存文件?(Node.js)

我有一个可能很大的文件(100+ Mb)的url,如何使用访存将其保存在本地目录中?

我环顾四周,但是关于如何执行此操作似乎没有很多资源/教程。

谢谢!

javascript xmlhttprequest fetch node.js fetch-api

9
推荐指数
4
解决办法
6440
查看次数

如何从ASP.NET Core正确下载文件到Angular?

我在服务器上有一个文件要发送给客户端:

[HttpPost]
public IActionResult Test()
{
    byte[] bytes = System.IO.File.ReadAllBytes(Path.Combine(FilesFolder, "test.docx"));
    return File(bytes, _contentTypeWord);
}
Run Code Online (Sandbox Code Playgroud)

我也尝试过

return PhysicalFile(pathUploadFile, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
Run Code Online (Sandbox Code Playgroud)

在客户端,我接受使用:

private _downloadFile(data: ArrayBuffer, fileName: string, contentType: string) {
    var blob = new Blob([data], { type: contentType });
    var url = window.URL.createObjectURL(blob);

    var link = document.createElement("a");
    link.setAttribute("href", url);
    link.setAttribute("download", fileName);
    link.style.display = "none";
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

public test() {
    this.http.post("Diplom/Test", { }, {
        headers: this.headers(),
    }).subscribe(
        result => {
            this._downloadFile(result.arrayBuffer(), "test.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
        },
        error => {
            alert("?? ??????? ??????????????? ????"); …
Run Code Online (Sandbox Code Playgroud)

c# typescript asp.net-core angular

1
推荐指数
2
解决办法
1万
查看次数