【发布时间】:2018-04-24 00:29:21
【问题描述】:
我目前正在尝试从我的 Angular 5 应用程序下载 Word 文档。 docx 文件位于服务器上的模板文件夹中,它从服务器打开时不会出现问题。但是当我在我的应用程序中检索它,将其发送到客户端,然后调用 saveAs 下载它时,它会下载,但文件打开时已损坏。我已经查看了一大堆其他问题,但还没有遇到涵盖这种情况的问题。我怀疑这可能是某种编码问题。
在我的控制器中,代码如下所示:
[HttpGet("[action]")]
public IActionResult GetGdwFile([FromBody] int gdwQueryJobId)
{
var currentDirectory = Directory.GetCurrentDirectory();
var filePath = Path.Combine(currentDirectory, @"DocumentTemplates\nab.docx");
var fileData = System.IO.File.ReadAllBytes(filePath);
return Ok(new Models.APIResult {Success = true, Data = fileData});
}
在 Angular 客户端代码中,我的调用如下所示:
downloadFile() {
this.gdwService.getGdwFile(1).subscribe(response => {
const fileBlob = new Blob([response.data], { type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document" });
saveAs(fileBlob, "Salesbypostcode.docx");
});
}
当我调试时,response.data 确实包含完整的二进制数据数组,正如预期的那样。
gdwservice 调用只是直接通过 http get:
getGdwFile(gdwQueryJobId: number) {
return this.http.get(`/api/Gdw/getGdwPdfFile?gdwQueryJobId=${gdwQueryJobId}`,
{
headers: new HttpHeaders({
"Content-Type": "application/json",
})
});
}
我需要做什么才能正确下载而不损坏文件?
【问题讨论】:
标签: .net angular filesaver.js