ASP.NET Excel和PDF下载,在my.PDF/.XLS扩展名中添加“[1]”
我有一个web表单页面,可以根据用户的选择将Crystal报表的内容写入pdf或excel文档 我有两个问题: 对于.pdf和.xls扩展名,都有一个带1的括号 在其内部附加到扩展名IE:fileName.xls 如果用户选择excel文档,在IE文件下载提示中 出现并询问是否要保存或打开文件: PDF文件保存得很好,但是如果我们选择打开excel文件,它会打开一个资源管理器窗口,我们会收到垃圾 这是我们的代码:ASP.NET Excel和PDF下载,在my.PDF/.XLS扩展名中添加“[1]”,asp.net,mime-types,Asp.net,Mime Types,我有一个web表单页面,可以根据用户的选择将Crystal报表的内容写入pdf或excel文档 我有两个问题: 对于.pdf和.xls扩展名,都有一个带1的括号 在其内部附加到扩展名IE:fileName.xls 如果用户选择excel文档,在IE文件下载提示中 出现并询问是否要保存或打开文件: PDF文件保存得很好,但是如果我们选择打开excel文件,它会打开一个资源管理器窗口,我们会收到垃圾 这是我们的代码: try { permission = new FileIOPermissi
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();
}
我一直在研究人们被附加到扩展中的现象,但是还没有发现太多关于它的信息,并且调试了代码并确认扩展是干净的
我的下一个猜测是Response.BinaryWritebyteArray中发生了什么;行或mime类型或内容标题
提前感谢您的帮助
道格我在我的项目中也做过类似的事情。 我已经在下面粘贴了我使用的代码。 我想,你不能换线。 Response.AppendHeader.xls,application/excel; 具有 Response.ContentType=应用程序/excel
string name = Path.GetFileName(path);
string ext = Path.GetExtension(path);
string type = "";
// set known types based on file extension
if (ext != null)
{
switch (ext.ToLower())
{
case ".htm":
case ".html":
type = "text/HTML";
break;
case ".txt":
type = "text/plain";
break;
case ".doc":
case ".docx":
case ".rtf":
type = "Application/msword";
break;
case ".pdf":
type = "application/pdf";
break;
case ".csv":
case ".xlsx":
type = "application/vnd.ms-excel";
break;
}
}
Response.AppendHeader("content-disposition",
"attachment; filename=" + name);
if (string.IsNullOrEmpty(type))
{
}
else
{
Response.ContentType = type;
}
Response.WriteFile(path);
Response.End();
将开关扩展到以下位置:
switch ("." + extn)
{
// case ".pdf":
// ...
default:
throw new NotImplementedException();
}
如果引发异常,这可能会提示您出了什么问题。您在哪里设置extn?extn=stringgrdReports.DataKeys[rowIndex][REPORTOUTPUTTYPE];尝试了这两种方法,但得到了相同的结果。Response.ContentType=application/vnd.ms-excel;Response.ContentType=应用程序/excel;