ASP.NET MVC FileResult是腐败的文件 [英] ASP.NET MVC FileResult is corrupting files

查看:246
本文介绍了ASP.NET MVC FileResult是腐败的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让我的ASP.NET MVC的网站一些数据导出为Excel文件。几个小时我以为NPOI只是生产垃圾,所以我切换到EPPlus。我在LINQPad测试,它创造了一个适当的工作XLSX文件,让我感动的code转移到MVC应用程序。再次,我得到的文件损坏。一次偶然的机会我正好看temp目录,看到由EPPlus创建的文件是3.87KB和完美的作品,但 FileResult 返回一个文件,该文件6.42KB,这已损坏。这究竟是为什么?我读的地方,它是服务器的GZip COM pression导致它,所以我把它关掉,它没有任何效果。有人,请帮帮我,我会在我心里......这是我的code。

  [HTTPGET]
公共FileResult Excel文件(
    CenturyLinkOrderExcelQueryModel查询){
    var文件= Manager.GetExcelFile(查询); //的FileInfo    返回文件(file.FullName,应用程序/ vnd.openxmlformats-officedocument.s preadsheetml.sheet,query.FileName);
}


解决方案

至于我担心有与 FileResult 的一个问题,它的伴随方法。我最终通过覆盖响应对象解决的问题:

  [HTTPGET]
公共无效的Excel(
    CenturyLinkOrderExcelQueryModel查询){
    var文件= Manager.GetExcelFile(查询);    Response.Clear();
    Response.ContentType =应用程序/ vnd.openxmlformats-officedocument.s preadsheetml.sheet
    Response.AddHeader(内容处置,附件;文件名=+ query.FileName);
    Response.BinaryWrite(System.IO.File.ReadAllBytes(file.FullName));
    Response.Flush();
    Response.Close();
    到Response.End();
}

I've been trying to get my ASP.NET MVC website to export some data as an Excel file. For hours I thought that NPOI was just producing garbage so I switched over to EPPlus. I tested it in LINQPad and it created a proper working XLSX file, so I moved the code over to the MVC app. AGAIN, I get corrupted files. By chance I happened to look at the temp directory and saw that the file created by EPPlus is 3.87KB and works perfectly, but the FileResult is returning a file that's 6.42KB, which is corrupted. Why is this happening? I read somewhere that it was the server GZip compression causing it, so I turned it off, and it had no effect. Someone, please help me, I'm going out of my mind... Here's my code.

[HttpGet]
public FileResult Excel(
    CenturyLinkOrderExcelQueryModel query) {
    var file = Manager.GetExcelFile(query); // FileInfo

    return File(file.FullName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", query.FileName);
}

解决方案

As far as I'm concerned there's an issue with the FileResult and it's accompanying methods. I ended up "resolving" the issue by overriding the Response object:

[HttpGet]
public void Excel(
    CenturyLinkOrderExcelQueryModel query) {
    var file = Manager.GetExcelFile(query);

    Response.Clear();
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + query.FileName);
    Response.BinaryWrite(System.IO.File.ReadAllBytes(file.FullName));
    Response.Flush();
    Response.Close();
    Response.End();
}

这篇关于ASP.NET MVC FileResult是腐败的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆