【问题标题】:Creating file works but when downloading that file nothing happens in C# [duplicate]创建文件有效,但是在下载该文件时,C#中没有任何反应[重复]
【发布时间】:2020-12-02 09:29:12
【问题描述】:

创建一个文件并上传它,我还想下载创建的文件。文件创建成功,但是当下载创建的文件时,我的以下代码没有任何反应。这是我的代码,在单击按钮时调用但是不能下载文件。还检查文件是否存在。它返回 true 但这不会给我任何错误,并且当此代码运行时没有任何反应。

[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" });
            }
        }
    }

文件创建代码

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

【问题讨论】:

  • 旁注:从一个动作(文件和 JSON)返回两种类型的响应时,您期望发生的事情非常令人困惑。也不清楚为什么您认为问题中需要文件创建代码...

标签: c# json download filestream


【解决方案1】:

使用 ajax 调用调用操作方法是下载文件的一种有点棘手的方法。以下代码帮助了我。

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

【讨论】:

  • 确保对问题提供良好的解释以及显示的代码如何解决它。到目前为止,它基本上只是“试试这个”,这不是有用的信息。这也确实不是第一次尝试通过 AJAX 下载文件 - 将问题标记为重复会更合适,特别是如果您对提供好的答案不感兴趣。
猜你喜欢
  • 2021-08-22
  • 2013-07-10
  • 2019-05-18
  • 2014-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-17
相关资源
最近更新 更多