【问题标题】:Changing the name in the header for a resource handler in C#在 C# 中更改资源处理程序的标头中的名称
【发布时间】:2008-10-30 18:52:42
【问题描述】:

我有一个资源处理程序,它是基于通过查询字符串传递的参数的 Response.WriteFile(fileName)。我正在正确处理 mimetype,但问题出在某些浏览器中,文件名显示为 Res.ashx(处理程序的名称)而不是 MyPdf.pdf(我正在输出的文件)。有人可以告诉我在将文件发送回服务器时如何更改文件名吗?这是我的代码:

// Get the name of the application
string application = context.Request.QueryString["a"];
string resource = context.Request.QueryString["r"];

// Parse the file extension
string[] extensionArray = resource.Split(".".ToCharArray());

// Set the content type
if (extensionArray.Length > 0)
    context.Response.ContentType = MimeHandler.GetContentType(
        extensionArray[extensionArray.Length - 1].ToLower());

// clean the information
application = (string.IsNullOrEmpty(application)) ?
    "../App_Data/" : application.Replace("..", "");

// clean the resource
resource = (string.IsNullOrEmpty(resource)) ?
    "" : resource.Replace("..", "");

string url = "./App_Data/" + application + "/" + resource;


context.Response.WriteFile(url);

【问题讨论】:

    标签: c# asp.net mime-types handler


    【解决方案1】:

    根据 Joel 的评论,您的实际代码如下所示:

    context.Response.AddHeader("content-disposition", "attachment; filename=" + resource);
    

    【讨论】:

      【解决方案2】:

      这篇 Scott Hanselman 的帖子应该会有所帮助:
      http://www.hanselman.com/blog/CommentView.aspx?guid=360

      【讨论】:

        【解决方案3】:

        谢谢大家的回答。最终代码工作并检查 pdf。

        if (extensionArray[extensionArray.Length - 1].ToLower() == "pdf")
            context.Response.AddHeader("content-disposition", 
                "Attachment; filename=" + resource);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-16
          • 1970-01-01
          • 2022-08-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多