创建文件是可行的,但是下载该文件时,C#中什么也没有

问题描述

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

confirmationWindowSize=0 
clientFailureCheckPeriod=30000
consumerWindowSize=0

文件创建代码

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

解决方法

使用ajax调用调用动作方法是下载文件的棘手方法。以下代码帮助了我。

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

HTML:

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...