extract blob from database

  • Thread starter Thread starter Selen
  • Start date Start date
S

Selen

I would like to be able to extract a BLOB from the database (SqlServer)
and pass it to a browser without writing it to a file. (The BLOB's
are word doc's, MS project doc's, and Excel spreadsheets.

How can I do this?
 
Hello,

I can help you with the first bit, extracting the blob from the database.

Create a SqlCommand to select it, e.g. "SELECT Blob FROM DataTable WHERE
<some criteria>". Then use ExecuteScalar to retrieve the result and cast it
to a byte array:
byte[] blob = (byte[])cmd.ExecuteScalar();

But I don't know what you need to do next, sorry. I'm sure somebody else
will know.
 
Response.Clear();
Response.AddHeader( "content-disposition", "attachment;
filename=\"Stuff.doc\"");
Response.ContentType = "application/msword";
// (or whatever the appropriate MIIME type)
Response.BinaryWrite( blob); // your byte array

// above is untested but you get the idea...
Your choices are "inline" or "attachment". If you choose "inline" then the
response
may be displayed in the browser if it so chooses
depending on the setting of the ContentType. If you choose "attachment" the
user will
always be prompted to save or open.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top