0

Creating a file and uploading it and i also want to download that file created .File is created successfully but when comes to download that created file nothing happens with my following code.This is my code which is called when button is clicked but unable to download file. Also checking file is present or not.It returns true but this don't give me any error and when this code is run nothing happens.

[HttpPost]
        public ActionResult DownloadGetOdds(string filename)
        {
            string filepath = Path.Combine(Server.MapPath("~/UploadFiles"), filename + ".json");

            if (file.FileExist(filepath) == true)
            {
                Response.Clear();
                Response.ClearHeaders();
                Response.ClearContent();
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename+".json");
                Response.Flush();
                Response.TransmitFile(Server.MapPath("~/UploadFiles/") +filename + ".json");
                Response.End();

                return Json(new { result = "SUCCESS" });
            }
            else
            {
                return Json(new {result = "Server Error" });
            }
        }
    }

File Creation Code

public string CreateJsonFile(string path, string data)
        {
            string status = "";
            try
            {
                using (StreamWriter file = File.CreateText(path))
                {
                    string _data = data;
                    JsonSerializer serializer = new JsonSerializer();
                    //serialize object directly into file stream
                    serializer.Serialize(file, _data);
                }
                status = "Successfully file created";
            }
            catch(Exception ex)
            {
                status = ex.Message;
            }
            return status;
        }
Minhaj
  • 25
  • 6
  • 1
    Side note: very confusing what you expect to happen from returning two types of responses from an action (file and JSON). Also not clear why you believe file creation code is necessary in the question... – Alexei Levenkov Aug 12 '20 at 15:43

1 Answers1

-1

Calling action methods with ajax calls is bit tricky way to download file.Following code helped me out.

public void DownloadOdds(string filename)
        {
            string filepath = Path.Combine(Server.MapPath("~/UploadFiles"), filename + ".json");

            if (file.FileExist(filepath) == true)
            {
                Response.Clear();
                Response.ClearHeaders();
                Response.ClearContent();
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".json");
                Response.Flush();
                Response.TransmitFile(Server.MapPath("~/UploadFiles/") + filename + ".json");
                Response.End();
            }
        }

Html:

<button class="btn btn-default" id="downloadOdsbtn" onclick="DownloadFile()">Download Odds</button>

function DownloadFile() {
        var file = $("#fileNameID").val();
        window.location.href = '@Url.Action("DownloadOdds", "Home", new { filename = "ff" })'.replace("ff", file);
    }
Minhaj
  • 25
  • 6
  • 1
    Make sure to provide good explanation of a problem and how code shown solves it. So far it is essentially just "try this" which is not a useful information. Also indeed this is not a first time one tried to download file via AJAX - flagging question as duplicate would be more appropriate especially if you are not interested in providing good answer. – Alexei Levenkov Aug 12 '20 at 15:46