c# - 在 C# 中更改资源处理程序 header 中的名称

标签 c# asp.net mime-types handler

我有一个资源处理程序,它是基于通过查询字符串传递的参数的 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);

最佳答案

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

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

关于c# - 在 C# 中更改资源处理程序 header 中的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/251307/

相关文章:

c# - 多级 web.config 转换

javascript - 将 mime Content-ID 添加到请求负载 (AJAX)

PHP 表单验证和 .srt MIME 类型?

c# - Linq,array.Contains() 生成异常 : Only primitive types ('such as Int32, String, and Guid' ) are supported in this context

c# - 将文件拖放到 wpf/C# 应用程序时,如何维护 Windows 资源管理器中的文件顺序?

c# - 在日期的自定义格式中使用撇号 ' '

html - 使用 Electron 对话框确定文件类型

c# - 获取 Windows Phone 8 中的电话号码

c# - 找不到类型或命名空间名称 'dynamic'

c# - 如何在 Web 用户控件 ascx 中调用 javascript 函数