I need to download a file in Asp.Net MVC application for which I have tried so many methods like returning File, FileResult, FileStreamResult but none of these works. I have also read hundreds of posts but none of those rectified my problem. I am calling my controller function through jquery ajax call. Upon success, I am getting alert message but the file is not downloading, also no exception or error is showing.
Here is my controller method:
public FileResult Download()
{
return File("C:/Users/Administrator/Documents/approve.png", "image/png");
}
And here is my ajax call:
$.ajax({
type: "GET",
url: '/Home/Download',
success:(function(response){
alert("Downloaded");
}),
error: (function () {
alert("Not Downloaded");
})
});
Remember, I have also tried other methods for file downloading too. What I am expecting is that system will download the file, but is just alerting a message "Downloaded", no any file downloads.
success:(function(response){ console.log(response);<- there is your downloaded file - it's downloaded into a javascript variable. It sounds like you're expecting the browser to (prompt to) save that file - why would it if you've only put inalert()? It's doing exactly what you asked: downloaded the file into a variable.