2008-12-06 12 views
9

No estoy seguro de cómo redactar esta pregunta ... ¡así que las ediciones son bienvenidas! De todos modos ... aquí va.¿Cómo se establece el nombre del archivo al transmitir un PDF en un navegador?

Actualmente, uso Crystal Reports para generar Pdfs y simplemente transmitir la salida al usuario. Mi código es el siguiente:

System.IO.MemoryStream stream = new System.IO.MemoryStream(); 

stream = (System.IO.MemoryStream)this.Report.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat); 

this.Response.Clear(); 
this.Response.Buffer = true; 
this.Response.ContentType = "application/pdf"; 
this.Response.BinaryWrite(stream.ToArray()); 
this.Response.End(); 

Después de ejecutar este código se transmite el PDF a la apertura del navegador hasta Acrobat Reader. ¡Funciona genial!

Mi problema es cuando el usuario intenta guardar el archivo por defecto al nombre del archivo real ... en este caso se predetermina a CrystalReportPage.pdf. ¿Hay alguna manera de configurar esto? ¿Si es así, cómo?

Cualquier ayuda sería apreciada.

Respuesta

17

Por lo general, a través de la cabecera Content-Disposition - es decir

Content-Disposition: inline; filename=foo.pdf 

Entonces sólo tiene que utilizar Headers.Add o AddHeader, o lo que sea, con:

response.Headers.Add ("Contenido -Disposition "," inline; filename = foo.pdf ");

(en línea es "mostrar en el navegador", el apego es "guardar como un archivo")

7
System.IO.MemoryStream stream = new System.IO.MemoryStream(); 

stream = (System.IO.MemoryStream)this.Report.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat); 

this.Response.Clear(); 
this.Response.Buffer = true; 
this.Response.ContentType = "application/pdf"; 
this.Response.AddHeader("Content-Disposition", "attachment; filename=\""+FILENAME+"\""); 
this.Response.BinaryWrite(stream.ToArray()); 
this.Response.End(); 
Cuestiones relacionadas