小编Жуэ*_*туа的帖子

如何从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万
查看次数

标签 统计

angular ×1

asp.net-core ×1

c# ×1

typescript ×1