简体   繁体   English

在 mvc 中导出到 CSV 不起作用

[英]Export to CSV in mvc not work

I want to export a list to CSV file using MVC, Jquery.我想使用 MVC、Jquery 将列表导出到 CSV 文件。 I want to write to a file to the response stream but doesn't do anything.我想将文件写入响应流,但什么也不做。 No Download appear.没有下载出现。

Below is my code in .cshtml下面是我在 .cshtml 中的代码

 <button id="btnGenerateCSV">Generate CSV</button>

in .js在 .js 中

 $('#btnGenerateCSV').click(function () {
   $.post(root + "ClaimInternal/DownloadCsv");
 });

In myController在我的控制器中

         private string GetCsvString(IList<Area> Area)
         {
            StringBuilder csv = new StringBuilder();

            csv.AppendLine("AreaID,AliasName,Description,BankName");

            foreach (Area area in Area)
            {
                csv.Append(area.AreaID + ",");
                csv.Append(area.AliasName + ",");
                csv.Append(area.Description + ",");
                csv.Append(area.BankName + ",");

                csv.AppendLine();
            }

            return csv.ToString();
        }


        public void DownloadCsv()
        {
            using (Database db = new Database(_ConnectionString))
            {
                AreaDAC dacArea = new AreaDAC(db);
                List<Area> data = dacArea.Search("", "", "");
                string facsCsv = GetCsvString(data);

                // Return the file content with response body. 
                Response.ContentType = "text/csv";
                Response.AddHeader("Content-Disposition", "attachment;filename=Area.csv");
                Response.Write(facsCsv);
                Response.End();
                }
        }

I try to make like this also我也试着这样做

       public FileContentResult DownloadCsv()
        {
            using (Database db = new Database(_ConnectionString))
            {
                AreaDAC dacArea = new AreaDAC(db);
                List<Area> data = dacArea.Search("", "", "");

                JsonResult result = new JsonResult();
                result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                result.Data = data;

               string facsCsv = GetCsvString(data);
               return File(new System.Text.UTF8Encoding().GetBytes(facsCsv), "text/csv", "Area.csv");
            }
        }

I make also like this我也这样

            public void DownloadCsv()
            {
                using (Database db = new Database(_ConnectionString))
                {
                    AreaDAC dacArea = new AreaDAC(db);
                    List<Area> data = dacArea.Search("", "", "");
                    string facsCsv = GetCsvString(data);

                    // Return the file content with response body. 
                                   Response.Clear();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", "attachment;filename=Area.csv");
                Response.Charset = "";
                Response.ContentType = "application/text";
                Response.Output.Write(facsCsv);
                Response.Flush();
                Response.End();
                    }
            }

But still not work.. No download CSV appear.. While When I got it all working, no error.但仍然不起作用.. 没有下载 CSV 出现.. 当我一切正常时,没有错误。

Now I already got the answer.现在我已经得到了答案。 I change the script like this and it's work..我像这样更改脚本,它的工作..

var cd = new System.Net.Mime.ContentDisposition
                {
                    FileName = "Area.csv",
                    Inline = true,
                };
                Response.AppendHeader("Content-Disposition", cd.ToString());
                return File(new System.Text.UTF8Encoding().GetBytes(facsCsv), "text/csv");

It's work by adding new System.Net.Mime.ContentDisposition.它的工作原理是添加新的 System.Net.Mime.ContentDisposition。 But I'm still not understand why.但我还是不明白为什么。

I was able to do using我能够使用

 window.location = "/urlPath?filename=" + filename;

and in my controller在我的控制器中

            filename = filename.ToString();
            byte[] fileBytes = System.IO.File.ReadAllBytes(filename);
            var response = new FileContentResult(fileBytes, "application/octet-stream");
            response.FileDownloadName = "Filename.csv";


            return response;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM