Write PDF or Word Document from Byte Array To Memory Stream

Once you store a Word Document or PDF Document in your database as a BLOB you then need to convert it to a Byte Array in order for the browse to launch Word or Adobe to open the PDF. This example pulls either the Word or PDF version from a table. 

//Select either the Word or PDF version
byte[] ProposalDoc;
string ContentType = string.Empty;
string Extension = string.Empty;

if (asWord)
{
    ProposalDoc = row.PROPOSAL_WORD;
    ContentType = "application/word";
    Extension = ".doc";
}
else
{
    ProposalDoc = row.PROPOSAL_PDF;
    ContentType = "application/pdf";
    Extension = ".pdf";
}

MemoryStream MemStream = new MemoryStream(ProposalDoc);
CTX.Response.ContentType = ContentType;
CTX.Response.AddHeader("content-disposition", 
       "attachment; filename=" + DocumentName + Extension);
//Response.AddHeader("Content-Length", oStream.Length.ToString());

CTX.Response.Clear();
CTX.Response.Buffer = true;
CTX.Response.BinaryWrite(MemStream.ToArray());
CTX.Response.End();            
Comments are closed