1
I have files that were uploaded to wwwroot/files/ folder of my project. I want to download these files but when I click on the button nothing happens.
public ActionResult DownloadFile(string arquivo)
{
if (arquivo == null)
return Content("filename not present");
var path = Path.Combine(
Directory.GetCurrentDirectory(),
"wwwroot", "arquivos", arquivo);
var memory = new MemoryStream();
var writer = new StreamWriter(memory);
writer.Flush();
memory.Position = 0;
return File(memory, GetContentType(path), arquivo);
}
html
<li>Download:
<a class="btn btn-primary mb-3" onclick="downloadMaterial(this)" [email protected](modelItem => item.Arquivo)><span class="glyphicon glyphicon-download-alt"></span></a>
</li>
function downloadMaterial(e){
var path = e.name;
var arquivo = path.split("arquivos/")[1];
$.ajax({
url: '@Url.Action("DownloadFile")',
type: "POST",
cache: false,
data: {'arquivo': arquivo},
});
}
Thanks, now you are downloading. However the file is coming empty...what could be the error in my controller? I can’t see in my Action the moment I pick up the file, I just see getting the file path.. Or this is done in the Memorystream part?
– Bianca C.
the Path is correct?
– Barbetta
Yeah, I already checked
– Bianca C.
Another question, is net core?
– Barbetta
That’s right!....
– Bianca C.
I edited with an alternative
– Barbetta
It worked out! Thank you
– Bianca C.