【发布时间】:2013-04-06 07:42:40
【问题描述】:
我有一个 Web 表单页面,它应该能够根据用户所做的选择将 Crystal Report 的内容写入 pdf 或 excel 文档。
我有两个问题:
对于 .pdf 和 .xls 扩展名,都有一个带 1 的括号 在它里面被附加到扩展IE:fileName.xls'1'
如果用户选择excel文档,在IE中会出现文件下载提示 出现并询问我们是否要保存或打开文件:
PDF 文件保存得很好,但是如果我们选择打开 excel 文件,它会打开一个资源管理器窗口并且我们会得到垃圾...
这是我们的代码:
try
{
permission = new FileIOPermission(FileIOPermissionAccess.Read, output);
fs = File.Open(output, FileMode.Open, FileAccess.Read);
byte[] byteArray = new byte[fs.Length];
fs.Read(byteArray, 0, (int)fs.Length);
fs.Close();
fs.Dispose();
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
fileName = fileName + '.' + extn;
//Response.AddHeader("Content-Disposition", "attachments;filename=\"" + fileName + ".\"" + extn);
Response.AddHeader("Content-Disposition", "attachments;filename=\"" + fileName + "\"");
Response.AddHeader("Content-Length", byteArray.Length.ToString());
switch ("." + extn)
{
case ".pdf":
Response.AppendHeader("Content-Type", "application/pdf");
break;
case ".xls":
//Response.ContentType = "application/vnd.ms-excel";
//Response.ContentType = "application/octet-stream";
Response.AppendHeader(".xls", "application/excel");
//Response.AppendHeader("Content-Type", "application/vnd.ms-excel");
//Response.AppendHeader("Content-Type", "application/force-download");
break;
case ".txt":
Response.AppendHeader("Content-Type", "application/notepad");
break;
}
FileInfo info = new FileInfo(output);
info.Delete();
Response.BinaryWrite(byteArray);
Response.Flush();
Response.End();
}
catch (Exception ex)
{
if (fs != null)
fs.Close();
}
我一直在研究人们遇到“1”被附加到扩展程序的现象,但没有发现太多关于它的信息,并已调试代码并确认扩展程序是干净的。
我的下一个猜测是 Response.BinaryWrite(byteArray);行或 mime 类型或内容标题...
提前感谢您的帮助
道格
【问题讨论】:
-
你在哪里设置
extn? -
extn = (string)grdReports.DataKeys[rowIndex]["REPORTOUTPUTTYPE"];
标签: asp.net mime-types