Open pdf / doc stored on sql server using asp.net and entityframework

0

I have a web application in which I am saving files of type pdf or doc in the database in a table with a parameter of type "varbinary", I know that it is not ideal to save such files in the database but I need to know how I can see that file from my web browser in a new tab, I was looking at binaryreader but it is not clear to me yet, I would appreciate your help!

    
asked by Roberto Antonio Rico Palma 21.06.2018 в 22:10
source

1 answer

1

You can solve it in the following way:

//Obtenemos el byte[]
var bytes = tuEntity.ToByteArrayPropery;
//Limpiamos el response para quitarnos todo el HTML 
Response.Clear()
//Seteamos el content type
//Esta parte debería ser dinámica si tienes más de un tipo
Response.ContentType = "application/pdf";
//Opcional, seteamos el file name
Response.AddHeader("content-disposition", "attachment;filename=MyPDF.pdf");
//Escribimos el output
Response.OutputStream.Write(bytes, 0, bytes.Length);
//Tambien se puede hacer así
Response.BinaryWrite(bytes)
//Finalizamos el response
Response.End()
    
answered by 22.06.2018 / 13:34
source