在C#中更改资源处理程序头部的名称

3

我有一个资源处理程序,它基于通过查询字符串传递的参数来进行Response.WriteFile(fileName)。我已经正确处理了MIME类型,但问题是在某些浏览器中,文件名显示为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);
3个回答

4

延伸Joel的评论,你的实际代码应该像这样:

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

1

0

谢谢大家的答复,最终的代码可以工作并检查pdf文件。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接