0

I have tried several way to download. searched several solution in SO, CP or google. I am not seeing any error or compiler is running it fine. browser is acting good. Just download is not working. Here is the downlaod code part.

.cs file

[HttpGet]
    public ActionResult Download()
    {
        string[] s = new string[2];
        using (StreamReader sr = new StreamReader(Server.MapPath("~\\test.txt")))
        {
            int i = 0;
            while (sr.Peek()>=0)
            {
                s[i] = sr.ReadLine();
                i++;
            }
        }
        return File(s[0], "application/zip", s[1]);
    }

.cshtml

<input type="button" id="btnDownload" value="Download" />

javascript code

$("#btnDownload").click(function () {

            $.ajax({
                url: '@Url.Action("Download", "Image")',
                type: 'GET',

                async: false,
                cache: false,

                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (data) {
                    alert(1);
                    window.location = '@Url.Action("Download", "Image")';
                }
            });
        });

it does not come to success alert in ajax request. goes to Download method. text file is ok. and filename and path is correct as well.

But I think, anything is wrong in the line

return File(s[0], "application/zip", s[1]);
Abdur Rahim
  • 3,975
  • 14
  • 44
  • 83
  • 1
    Not really clear what your trying to do here. Why do you have an ajax call to the `Download()` method? You specify `contentType: 'application/json'` which is pointless in a GET, and you specify `dataType: 'json'` but the method does not return json, so a `500 (Internal Server Error)` would be thrown. And since you cannot return a file using ajax, your code in the `.click()` handler should just be one line of code - `window.location = '@Url.Action("Download", "Image")';` –  Mar 14 '17 at 23:50
  • @StephenMuecke Excellent analysis and description of the solution. I have solved. can you put it as answer? – Abdur Rahim Mar 15 '17 at 00:04

0 Answers0