I have checked many questions on StackOverflow, and also searched on many different websites for possible solutions, but I don't seem to find any answer. Some don't work, others are very old and don't work either.
I have saved .pdf files in SQL Server as binary. Seems to work. But I don't know how to retrieve the data and turn the byte[] back to the .pdf file and make it available for download.
This is my model, where I save the .pdf file in byte[]:
public class Language
{
public int Id { get; set; }
public string Name { get; set; }
public byte[] Certificate { get; set; }
}
This is how I upload into database:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(string name, IFormFile certificate)
{
Language language = new Language()
{
Name = name
};
if(certificate.Length > 0)
{
using (var stream = new MemoryStream())
{
await certificate.CopyToAsync(stream);
language.Certificate = stream.ToArray();
}
}
_context.Add(language);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
I would like it to be able to retrieve the data and then download it.
I appreciate any help.