我使用的是WINDOWS 7,IIS 7 Express 我想从服务器下载excel .xlsx文件(2013)。它可以下载但不能打开。使用xls时同样的问题。我错过了什么? 这是代码:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string mediaName = HttpContext.Current.Request.QueryString["n"].ToString();
if (string.IsNullOrEmpty(mediaName))
{
return;
}
string destPath = context.Server.MapPath("~/EXCELtemplates/" + mediaName);
// Check to see if file exist
FileInfo fi = new FileInfo(destPath);
try
{
if (fi.Exists)
{
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fi.Name);
HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());
//HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; // (xlsx)
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.TransmitFile(fi.FullName);
HttpContext.Current.Response.Flush();
}
}
catch (Exception exception)
{
HttpContext.Current.Response.ContentType = "text/plain";
HttpContext.Current.Response.Write(exception.Message);
}
finally
{
HttpContext.Current.Response.End();
}
}