No, you cannot download files using an AJAX request. You need to redirect the browser to the target url instead of sending an AJAX request:
function ViewFile(url, cnt) {
window.location.href = '@(Url.Action("Download"))?' +
'url=' + encodeURIComponent(url) +
'&cnt=' + encodeURIComponent(cnt);
}
Also bear in mind that the File
function expects as first argument an absolute physical path to the file such as:
public ActionResult Download(string url, string cnt)
{
if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(cnt) && File.Exists(url))
{
return File(url, cnt);
}
return HttpNotFound();
}
Also if you want to prompt for the file to download you need to specify a filename (3rd argument of the File function):
return File(@"c:\reports\foo.pdf", "application/pdf", "foo.pdf");