Sanfoor Sanfoor

Sanfoor Sanfoor

  • NA
  • 6
  • 12.3k

retrive binary file

Jan 4 2016 3:56 PM

i have a function that retrieve a document (PDF,doc,docs) or image as binary array.

and i have a link button.

how i could open this document/image in a new window when clicking on the link button

here is a sample code?

byte[] bArrAttachment = null;
bArrAttachment = obj.RetriveDocument(nDocumentID,out sFileExtension);
lnkShowScan.Visible = true;
if (bArrAttachment != null)
{
Response.ClearContent();
Response.ClearHeaders();
string contentType = "";
// set known types based on file extension
if (sFileExtension != null)
{
switch (sFileExtension.ToLower())
{
case ".htm":
case ".html":
contentType = "text/HTML";
break;
case ".txt":
contentType = "text/plain";
break;
case ".pdf":
contentType = "Application/pdf";
break;
case ".doc":
case ".rtf":
case ".docx":
case ".dot":
contentType = "Application/msword";
break;
case ".xls":
case ".xlt":
case ".xla":
case ".csv":
contentType = "application/vnd.ms-excel";
break;
case ".ppt":
case ".pot":
case ".pps":
case ".ppa":
contentType = "application/vnd.ms-powerpoint";
break;
case ".jpg":
case ".jpeg":
case ".gif":
case ".bmp":
case ".png":
case ".tif":
case ".tiff":
contentType = "image/Png";
break;
}
}
Response.OutputStream.Write(bArrAttachment, 0, bArrAttachment.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=file" + sFileExtension.ToLower() + "");
Response.Flush();
Response.End();
}
}

}

Answers (1)