【发布时间】:2018-06-30 04:24:46
【问题描述】:
我试图弄清楚如何将文件从我的 web api 传输到我的 angular 5 前端并下载它。
我的 Angular 5 前端很简单(我正在使用 FileSaver 包来提供帮助):
文件下载.component.html
<button (click)="downloadFile(document)">Download</button>
文件下载.component.ts
downloadFile(document) {
this.http.get("http://localhost:8080/"+document.id+).subscribe(
data => {
const dataFile = data;
saveAs(dataFile, document.filename);
},
err => { console.info(err); }
);
}
在我的 web api 上,我不知道如何构建响应。 到目前为止,我只有:
[HttpGet("{id}"]
public async Task<IActionResult> GetFile(Guid id) {
var testFileInfo = _dbcontext.UploadedFile.GetById(id);
var filePath = Path.Combine("C:\FileRepo", testFileInfo.Filename);
// what do i do here? i got no clue
throw new NotImplementedException();
}
我尝试过在线尝试各种示例,但似乎没有任何效果。
这个想法是 web api 可以将任何范围的文件返回到前端,这取决于服务器上的内容。
现阶段文件大小从 100kb 到 50mb 不等,一旦我实施多个文件的归档和压缩,文件可以更大。
【问题讨论】:
标签: asp.net angular asp.net-core asp.net-core-2.0