更新时间:2023-01-18 14:07:48
根据简短的网页搜索,word和excel的正确MIME类型是:
According to a brief web search, the correct mime types for word and excel are:
application/vnd.openxmlformats-officedocument.wordprocessingml.document
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
http://www.bram.us/2007/05/25/office-2007-mime-types-for-iis/
编辑:
以下简化示例适用于我。它与你的不同之处在于它使用通用处理程序而不是Web表单(这更适合这样的东西)。
The following simplified sample works for me. It is different from yours in that it uses a generic handler instead of a web form (which is more appropriate for something like this anyway).
要测试它,请确保在应用程序的***文件夹中有一个名为Book1.xlsx的excel 2007文件。
To test it, make sure there is an excel 2007 file named Book1.xlsx in the top level folder of the application.
DownloadSpreadsheet.ashx:
<%@ WebHandler Language="C#" Class="DownloadSpreadsheetHandler" %>
using System;
using System.Web;
using System.IO;
public class DownloadSpreadsheetHandler: IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
string path = context.Server.MapPath("~/Book1.xlsx");
using (FileStream spreadsheet = File.OpenRead(path))
{
CopyStream(spreadsheet, context.Response.OutputStream);
}
}
public bool IsReusable {
get {
return false;
}
}
private static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[32768];
while (true)
{
int read = input.Read(buffer, 0, buffer.Length);
if (read <= 0)
return;
output.Write(buffer, 0, read);
}
}
}