简体   繁体   English

FileContentResult操作产生不完整的文件

[英]FileContentResult action yielding incomplete file

I've been struggling with and researching this one all day. 我一直在努力研究这一天。 Fingers crossed that someone here can help me. 手指交叉,有人在这里可以帮助我。 :) :)

Preface: I am very, very new to ASP, so I apologize for my ignorance in advance. 前言:我对ASP非常非常陌生,因此我对自己的无知深表歉意。

Objective: I want to be able to return generated reports in various formats to users via download buttons. 目标:我希望能够通过下载按钮以各种格式将生成的报告返回给用户。 The reports are generated fine, and I've got them in string format ready to be shuffled off to populate a file somewhere. 报告生成的很好,我已经准备好以字符串格式将其改编为在某个地方填充文件了。

We're using razor MVC for our pages, and thus I've got an ActionLink for the download, like so: 我们的网页使用的是剃须刀MVC,因此,我有一个ActionLink可供下载,如下所示:

@Html.ActionLink("CSV", "export", (controller), new {
                        format = "text/csv",
                        #SNIP VARIOUS USER PARAMETERS#
                    }, null)

This posts to our controller: 这发布到我们的控制器:

[ActionName("export")]
[HttpGet]
public System.Web.Mvc.ActionResult ExportClubs(string format, #PARAMETERS#)
    {
        string fileName = "export" + ".csv";

        #GENERATE REPORT DATA#

        string content = #REPORT DATA#
        // Tried a few different encodings here.
        byte[] bytes = Encoding.UTF8.GetBytes(content);

        // many different header configurations tried here too; this is where the examples
        // tend to vary the most.
        HttpContext.Current.Response.AddHeader("content-disposition",
                "attachment; filename=" + fileName);
        HttpContext.Current.Response.ContentType = format;

        // Tried many different encodings here as well, including text/csv
        System.Web.Mvc.FileContentResult file = new System.Web.Mvc.FileContentResult(bytes, System.Net.Mime.MediaTypeNames.Text.Plain);
        file.FileDownloadName = fileName;
        return file;
    }

So, with a click, the file comes over with the correct name and everything! 因此,只需单击一下,文件就会带有正确的名称和所有内容! Except its contents looks like this: 除了它的内容看起来像这样:

{"FileContents":"RGlzd...#SNIP VERY LONG STRING#","ContentType":"text/plain","FileDownloadName":"export.csv"}

It seems like there's a final step being missed, somewhere, of taking that FileContents and reassembling a file out of it, but I can't for the life of me figure it out. 似乎在某个地方缺少最后一步,那就是获取FileContents并从中重新组装文件,但是我一生都无法解决。 Absolutely no examples I've seen address the matter, so I feel like I must have just missed something silly somewhere. 绝对没有我能看到的例子解决这个问题,所以我觉得我一定只是在某个地方错过了一些愚蠢的事情。

Thanks in advance! 提前致谢!

Your content Type is wrong, the last portion of your code when you instantiate file content result class should be like this: 您的内容类型错误,实例化文件内容结果类时代码的最后一部分应如下所示:

// Tried many different encodings here as well, including text/csv
System.Web.Mvc.FileContentResult file = new System.Web.Mvc.FileContentResult(bytes, "text/csv"); // or what you recieve from arguments
file.FileDownloadName = fileName;
return file;

Hope that helps ! 希望有帮助!

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

相关问题 找不到文件时处理FileContentResult - Handling FileContentResult when file is not found FileContentResult 返回 excel 文件损坏 - FileContentResult return excel file corrupt 使用FileContentResult返回文件并将其写入文件 - Getting file returned using FileContentResult and writing it to a file C#FileContentResult文件下载到自定义文件夹中 - C# FileContentResult File Download In Custom Folder 从AJAX帖子中调用时,FileContentResult不生成文件 - FileContentResult not generating file when called from AJAX post 将视频文件作为“FileContentResult”流式传输:无法转发/后退视频内容 - Streaming video file as 'FileContentResult': Not able to forward/backward the video content 为什么我无法使用 JQuery 和 FileContentResult 打开 CSV 文件 - Why I cannot open a CSV file using JQuery and FileContentResult 如何在 ASP.NET WebAPI 中返回文件 (FileContentResult) - How to return a file (FileContentResult) in ASP.NET WebAPI FileResult 和 FileContentResult 会影响文件的编码吗? C# - does the FileResult and FileContentResult affect the encoding of the file ? C# 从字节数组返回 PDF 作为 FileContentResult 产生无效文件 - Returning PDF as FileContentResult from byte array produces invalid file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM