I am trying to figure out how i can transfer a file from my web api to my angular 5 front end and download it.
My Angular 5 front end is simple (i am using FileSaver package to help):
filedownload.component.html
<button (click)="downloadFile(document)">Download</button>
filedownload.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); }
);
}
On my web api i have no idea how to construct the response. So far i only have:
[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();
}
I have tried experimenting with various examples online but nothing seems to work.
The idea is that web api would serve any range of files back to the front end, depending on whats on server.
File sizes range from 100kb to 50mb at this stage, file can be bigger once i implement archiving and zipping of multiple files.