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;
}