Kev*_*vin 13 c# asp.net excel ms-word
我正在开发一个Web应用程序,需要传输各种文件.我可以做pdf,图像和旧的Office文档.但是,当我尝试使用2007文档时,它会中断.这是我的代码:
Response.Buffer = true;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
switch (FileExtension.ToLower())
{
case "pdf":
Response.ContentType = "application/pdf";
break;
case "doc":
Response.ContentType = "application/msword";
break;
case "docx":
Response.ContentType = "application/vnd.ms-word.document.12";
break;
case "xls":
Response.ContentType = "application/vnd.ms-excel";
break;
case "xlsx":
Response.ContentType = "application/vnd.ms-excel.12";
break;
default:
Response.ContentType = "image/jpeg";
break;
}
Response.BinaryWrite(buffer);
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
An invalid character was found in text content. Error processing resource 'http://DomainName/GetFile.aspx... PK
有什么建议?
Erv*_*ter 16
根据简短的网络搜索,word和excel的正确mime类型是:
application/vnd.openxmlformats-officedocument.wordprocessingml.document
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Run Code Online (Sandbox Code Playgroud)
http://www.bram.us/2007/05/25/office-2007-mime-types-for-iis/
编辑:
以下简化示例适用于我.它与您的不同之处在于它使用通用处理程序而不是Web表单(无论如何它更适合这样的事情).
要对其进行测试,请确保在应用程序的顶级文件夹中有一个名为Book1.xlsx的excel 2007文件.
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
.doc
application/msword
.dot
application/msword
.docx
application/vnd.openxmlformats-officedocument.wordprocessingml.document
.dotx
application/vnd.openxmlformats-officedocument.wordprocessingml.template
.docm
application/vnd.ms-word.document.macroEnabled.12
.dotm
application/vnd.ms-word.template.macroEnabled.12
.xls
application/vnd.ms-excel
.xlt
application/vnd.ms-excel
.xla
application/vnd.ms-excel
.xlsx
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xltx
application/vnd.openxmlformats-officedocument.spreadsheetml.template
.xlsm
application/vnd.ms-excel.sheet.macroEnabled.12
.xltm
application/vnd.ms-excel.template.macroEnabled.12
.xlam
application/vnd.ms-excel.addin.macroEnabled.12
.xlsb
application/vnd.ms-excel.sheet.binary.macroEnabled.12
.ppt
application/vnd.ms-powerpoint
.pot
application/vnd.ms-powerpoint
.pps
application/vnd.ms-powerpoint
.ppa
application/vnd.ms-powerpoint
.pptx
application/vnd.openxmlformats-officedocument.presentationml.presentation
.potx
application/vnd.openxmlformats-officedocument.presentationml.template
.ppsx
application/vnd.openxmlformats-officedocument.presentationml.slideshow
.ppam
application/vnd.ms-powerpoint.addin.macroEnabled.12
.pptm
application/vnd.ms-powerpoint.presentation.macroEnabled.12
.potm
application/vnd.ms-powerpoint.presentation.macroEnabled.12
.ppsm
application/vnd.ms-powerpoint.slideshow.macroEnabled.12
Run Code Online (Sandbox Code Playgroud)