下载使用Response.TransmitFile .xlsx文件() [英] Download .xlsx file using Response.TransmitFile()

查看:301
本文介绍了下载使用Response.TransmitFile .xlsx文件()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一些code生成一个Excel US preadsheet服务器端,然后将其下载到用户。我使用 ExcelPackage 来生成该文件。

I'm working on some code that generates an Excel spreadsheet server-side and then downloads it to the user. I'm using ExcelPackage to generate the file.

的生成是工作得很好。我可以使用Excel 2007中没有问题打开生成的文件。但是,我无法下载文件与 Response.TransmitFile()

The generation is working just fine. I can open the generated files using Excel 2007 with no issues. But, I'm having trouble downloading the file with Response.TransmitFile().

现在,我有以下的code:

Right now, I have the following code:

//Generate the file using ExcelPackage
string fileName = generateExcelFile(dataList, "MyReportData");

Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.ContentType = "application/vnd.xls"
Response.Charset = "";
Response.TransmitFile(fileName);

在Excel 2007中打开如上下载的文件,它提供了文件格式不匹配的扩展的警告。点击过去的警告后,Excel显示文件的原始XML内容。

When Excel 2007 opens the file downloaded as above, it gives the "file format doesn't match extension" warning. After clicking past the warning, Excel displays the raw xml contents of the file.

如果我更改文件扩展名,像这样

If I change the file extension, like so

Response.AddHeader("content-disposition", "attachment;filename=FileName.xlsx");

Excel 2007中给出了一个创先争优发现,在文件无法读取内容的错误,其次是提供以定位在网络上转换器的对话框。如果我点击否在此对话框上,Excel中的的能够加载数据。

我也用不同的MIME类型尝试,如应用程序/ vnd.ms - Excel中应用程序/ vnd.openxmlformats-officedocument.s preadsheetml.sheet ,结合.xls和的.xlsx的文件扩展名。所有组合产生上述两种行为之一

I've also experimented with different MIME types, like application/vnd.ms-excel and application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, combined with file extensions of .xls and .xlsx. All combinations result in one of the two behaviors mentioned above.

什么是文件扩展名和MIME类型的正确组合在这种情况下使用?还有什么可能导致此故障,不是一个不正确的MIME类型或扩展其他?

What is the correct combination of file extension and MIME type to use in this scenario? What else could cause this failure, other than an improper MIME type or extension?

仅供参考,这是与发生的Visual Studio的内置开发Web服务器。我还没有尝试过这种与IIS。

FYI, this is occurring with Visual Studio's built-in development web server. I haven't yet tried this with IIS.

推荐答案

我不能肯定地说,有什么不对的做法,但我只是分享做类似的东西了一些意见。

I can't definitely say that there's anything wrong with your approach, but I'll just share some observations from doing something similar.

头是帕斯卡情况下,大多数的浏览器不应该关心,但我想你的内容处置更改为内容处置。改变字符集不应该是必要的或相关的。你的内容类型应该没事,我只会用应用程序/ vnd.openxmlformats-officedocument.s preadsheetml.sheet和原来的.xlsx如果这实际上是文件的内容,否则与应用程序/ vnd.ms - Excel和坚持.xls的。

Headers are Pascal Case, most browsers shouldn't care but I would change your content-disposition to Content-Disposition. Changing the Charset shouldn't be necessary or relevant. Your content type should be fine, I would only use application/vnd.openxmlformats-officedocument.spreadsheetml.sheet and .xlsx if that is actually the content of the file, otherwise stick with application/vnd.ms-excel and .xls.

您应该考虑的是向浏览器发送的内容长度的另一件事情:

Another thing you should consider is sending the browser the Content-Length:

Response.AddHeader("Content-Length", new System.IO.FileInfo("FileName.xlsx").Length);

也有你多个浏览器尝试这样做?只是想知道如果它是一个特定供应商的问题。

Also have you tried this with multiple browsers? Just wondering if it's a vendor-specific problem.

作为最后的努力,您可以将您的内容类型设置为应用程序/八位字节流,任何浏览器应该提供下载它,然后大多数浏览器将让你打开它,它是基于扩展下载后。

As a last ditch effort, you can set your Content-Type to application/octet-stream, and any browser should offer to download it, and then most browsers will let you open it after it's downloaded based on the extension.

这篇关于下载使用Response.TransmitFile .xlsx文件()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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