How to send a binary block to a client browser?

Forgive the dumb question for the newbies here; web programming is not my forte ... (blush)

I have an aspx page running on a web server. I have a blob (byte array) containing any type of binary, plus the file name.

I want this file to be downloaded through the browser to the client and opened using any default application for this type of file. I really don't want to save blob as a file on the server; which will leave a terrible mess in the economy, which I just do not want to think about.

I tried looking for this question, but I assume that I am using the wrong keywords.

It really should be obvious how to do it, but I have no joy.

What is the trick?

Thanks!

0
source share
2 answers
Response.BinaryWrite(byteArray);

You must also set the content type.

Response.ContentType = "application/pdf";

But this will be based on your file type.

And the file name (and all together) is as follows

Response.AddHeader("content-disposition", 
   String.Format("attachment;filename={0}", fileName));    
Response.ContentType = "application/pdf";
Response.BinaryWrite(byteArray);
+10
source

First, you must know the mime type. Once you know this, you can set the Response.ContentType property. After that, just use Response.BinaryWrite (). If you have not set the ContentType property, the client has virtually no chance of opening the file correctly.

+3
source

Source: https://habr.com/ru/post/1704148/


All Articles