1

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.

7
  • 1
    It is working as intended. You tell it to download a file through AJAX, not as a regular browser download. It does it and gives you the data. If you want it to be an actual download you have to tell the browser to go to the URL you want, not use AJAX Commented Sep 19, 2019 at 6:35
  • 1
    All of the "hundreds of posts" did rectify your problem - they all say it can't be done via AJAX as javascript doesn't have a way to tell the browser to save the file it downloaded. Commented Sep 19, 2019 at 6:39
  • 1
    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 in alert()? It's doing exactly what you asked: downloaded the file into a variable. Commented Sep 19, 2019 at 7:04
  • @SamiKuhmonen, is there a way to download a file without using ajax? Commented Sep 20, 2019 at 6:32
  • 1
    Sure, just direct the browser to the URL where the file is, and on the server side add headers to indicate it’s meant to be saved as a file (content-disposition header). So basically use window.location to set the URL you want and the browser will handle the rest Commented Sep 20, 2019 at 6:35

1 Answer 1

2

In controller,

public FileResult Download()
            {
                return Json("C:/Users/Administrator/Documents/approve.png");
            }

In ajax

$.ajax({
        type: "GET",
        url: '/Home/Download',
         dataType: "json",
        success:(function(response){
            window.open(response, '_blank');
            alert("Downloaded");
        }),
        error: (function () {
            alert("Not Downloaded");
        })
    });
Sign up to request clarification or add additional context in comments.

3 Comments

I tried it, but not working. I have also tried it by including Json behaviour to allow get, but not works as well.
did you get response in success ajax ?
I have done it. First, I have changed the return type to JsonResult and then added Content-Disposition header. Then in JS, i have modified your code to window.open(encodeURIComponent(response), '_blank'); It is working fine now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.