【发布时间】:2018-07-29 14:10:50
【问题描述】:
我在服务器上有一个文件要发送给客户端:
[HttpPost]
public IActionResult Test()
{
byte[] bytes = System.IO.File.ReadAllBytes(Path.Combine(FilesFolder, "test.docx"));
return File(bytes, _contentTypeWord);
}
我也试过
return PhysicalFile(pathUploadFile, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
在我接受的客户处:
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("Не удалось отредактировать файл");
console.error(JSON.stringify(error));
})
}
客户端收到的文件已损坏,无法打开。服务器上的文件很好,可以完美打开。生成的文件仍然无法打开,甚至重 2 倍(服务器上有 487 KB,客户端有 925 KB)。
【问题讨论】:
-
尝试在后端和客户端之间发送 base64 编码。
标签: c# angular typescript asp.net-core