【问题标题】:I need to put .xlsx to download [duplicate]我需要把.xlsx 下载[重复]
【发布时间】:2014-01-16 13:08:25
【问题描述】:

我正在尝试将文件 .xlsx 下载。我正在使用 library.cs。当我使用 MVC 3 时,我会使用它:

HttpContext.Response.Clear();
HttpContext.Response.AppendHeader("Content-Disposition", contentDisposition.ToString());
return File(pck.GetAsByteArray(), "application/octet-stream");

但现在,有了 .cs,我不知道该怎么做。

【问题讨论】:

标签: c# asp.net xlsx


【解决方案1】:

试试这个:

public void DownloadExcel(byte[] buffer, string nameFile) {

    // Verify invalid chars on nameArchive parameter:
    var preparedName = new String(
       nameFile.Where(c => !Path.GetInvalidFileNameChars().Contains(c)).ToArray()
    );

    if (String.IsNullOrEmpty(preparedName))
       preparedName = "DefaultName";

    HttpResponse response = HttpContext.Current.Response;
    response.Clear();
    response.ContentType = 
       "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    response.AddHeader("Content-Disposition", 
       String.Format("attachment;filename={0}.xlsx", preparedName));
    response.AddHeader("Content-Length", buffer.Length.ToString());
    response.BinaryWrite(buffer);
    response.End();
}

我认为你的问题是我的类型,尝试使用与上述方法相同的方法。

【讨论】:

  • 我认为您第一次使用的内容类型是正确的 - vnd.ms-excel 适用于 .xls 而不是 .xlsx。你不会转义nameArchive,以防它包含非ASCII,但如果它在应用程序的控制之下,那可能没问题。
  • 感谢@Rup,我根据您的评论改进了我的答案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-17
  • 1970-01-01
  • 2012-09-16
相关资源
最近更新 更多