1

我有一个 Web 表单页面,它应该能够根据用户所做的选择将 Crystal Report 的内容写入 pdf 或 excel 文档。

我有两个问题:

  1. 对于 .pdf 和 .xls 扩展名,都有一个括号,其中一个括号被附加到扩展名 IE:fileName.xls'1 '

  2. 如果用户选择一个 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 类型或内容标题...

提前感谢您的帮助

道格

4

2 回答 2

0

将您扩展switch到以下内容:

switch ("." + extn)
{
    // case ".pdf":
    // ...

    default:
        throw new NotImplementedException();
}

如果抛出异常,这可能会提示您出了什么问题。

于 2013-04-14T12:46:15.960 回答
0

我在我的项目中做过类似的事情。我已经粘贴了我在下面使用的代码。我想,你只需更换线路。Response.AppendHeader(".xls", "application/excel"); with 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();
于 2013-04-14T01:39:34.060 回答