Pergunta

Eu tenho um manipulador de recursos que é Response.WriteFile (fileName) com base em um parâmetro passado através da querystring. Eu estou lidando com o tipo MIME corretamente, mas a questão é, em alguns navegadores, o nome do arquivo aparece como Res.ashx (O nome do manipulador) em vez de MyPdf.pdf (o arquivo que estou saída). Alguém pode me informar como alterar o nome do arquivo quando ele é enviado de volta para o servidor? Aqui está o meu código:

// 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);
Foi útil?

Solução

Estendendo-se desde o comentário de Joel, seu código real seria algo parecido com isto:

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

Outras dicas

Este post Scott Hanselman deve ser útil:
http://www.hanselman.com/blog/CommentView.aspx?guid= 360

Obrigado a vocês pela sua resposta. As obras de código finais e cheques para pdf.

if (extensionArray[extensionArray.Length - 1].ToLower() == "pdf")
    context.Response.AddHeader("content-disposition", 
        "Attachment; filename=" + resource);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top