如何使用asp.net和c#流式传输Excel 2007或Word 2007文件
发布时间:2020-05-24 06:12:50 所属栏目:asp.Net 来源:互联网
导读:我正在开发一个网络应用程序,需要流式传输各种文件.我可以做PDF,图像和较旧的Office文档.但是,当我尝试与2007年的文件,它打破了.这是我的代码: Response.Buffer = true; Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); switch (FileE
|
我正在开发一个网络应用程序,需要流式传输各种文件.我可以做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);
我得到的错误是: An invalid character was found in text content. Error processing resource 'http://DomainName/GetFile.aspx... PK 有什么建议么? 解决方法根据简短的网页搜索,word和excel的正确的MIME类型是: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表单(这更适合于这样的东西). 要测试它,请确保应用程序的顶级文件夹中有一个名为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,buffer.Length);
if (read <= 0)
return;
output.Write(buffer,read);
}
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 如何显示UTC时间作为本地时间在网页?
- 在将所有websocket数据发送到客户端之前,ASP.NET关闭连接
- 在视图中确定ASP.NET Core环境名称
- asp.net-mvc – 在RedirectToRoute和RedirectToAction的ASP
- ASP.NET Forms Authentication阻止在Login.aspx上加载javas
- 什么是部署ASP.Net Web应用程序的好方法?
- asp.net – “连接字符串指定本地Sql Server Express实例”
- asp.net-mvc – 加载测试SignalR集线器应用程序的最佳方法是
- asp.net – HttpRuntime Cache和HttpContext Cache有什么区
- ASP.NET工作进程仍然以31kb的大小返回数据
推荐文章
站长推荐
- 如何缓存输出的action方法,返回图像到asp.net mv
- asp.net-mvc – 存储库与DAL中的服务模式:EF和D
- asp.net – 将JSON数据解析为.NET对象的最佳方式
- asp.net-mvc – 具有指定Controller和Action的Ht
- 如何打开一个页面在新的选项卡按钮点击asp.net?
- ASP.net MVC v2 – 调试模型绑定问题 – BUG?
- ASP.NET MVC Web应用程序与ASP.NET Web应用程序
- asp.net gridview:如何在一列中包含多个按钮字段
- asp.net – 配置部分不能在web.config文件中包含
- asp.net – 在Google Chrome浏览器中加载两次
热点阅读
