2008-10-30 4 views
3

У меня есть обработчик ресурсов, который является Response.WriteFile (имя_файла) на основе параметра, прошедшего через строку запроса. Я правильно обрабатываю mimetype, но проблема в некоторых браузерах, имя файла появляется как Res.ashx (имя обработчика) вместо MyPdf.pdf (файл, который я выводил). Может ли кто-нибудь сообщить мне, как изменить имя файла при его отправке обратно на сервер? Вот мой код:Изменение имени в заголовке для обработчика ресурсов в C#

// 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); 

ответ

4

Расширение от комментариев Иоиля, ваш фактический код будет выглядеть примерно так:

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

Спасибо ребята за ваш ответ. Окончательный код работает и проверяет PDF.

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

 Смежные вопросы

  • Нет связанных вопросов^_^