home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _6f1d34e6dba82b964ed051f52909341c < prev    next >
Text File  |  2004-06-01  |  2KB  |  64 lines

  1. <%@Language=PerlScript%>
  2. <%
  3. # Name:        readblob.asp
  4. # Author:    Tobias Martinsson
  5. # Description:    Opens a connection to SQL Server catalog pubs as the "sa"
  6. #        user. Executes a query which returns a BLOB and then reads
  7. #               the BLOB-content using GetChunk() and outputs it after it
  8. #               is converted to a VT_UI1 variant, which is a necessary type
  9. #               in the call to BinaryWrite().
  10.  
  11. use Win32::OLE::Variant;
  12.  
  13. # One note, 40 as bytes to read per GetChunk()-call is not a good number 
  14. # to choose for a real application. I emphasize _not_. Instead whatever
  15. # you choose depends much on your system and the power of it; however,
  16. # 4000 is a much more realistic number than 32.
  17. #
  18. my($blob_size, $read_size, $bytes_to_read) = (0, 0, 32);
  19.  
  20. # Let's create the Connection object used to establish the connection
  21. #
  22. $conn = $Server->CreateObject('ADODB.Connection');
  23.  
  24. # Open a connection using the SQL Server OLE DB Provider
  25. #
  26. $conn->Open(<<EOF);
  27.     Provider=SQLOLEDB;
  28.     Persist Security Info=False;
  29.     User ID=sa;Initial Catalog=pubs
  30. EOF
  31.  
  32. # Execute the query which returns the BLOB from our database
  33. #
  34. $rs = $conn->Execute("SELECT logo FROM pub_info WHERE pub_id='0736'");
  35.  
  36. # Get the size of the BLOB
  37. #
  38. $blob_size = $rs->Fields('logo')->{ActualSize};
  39.  
  40. # And here's the routine for reading in the blob. Alternatively you can
  41. # make a control statement that says if the $blob_size is less than 4096,
  42. # it should just swallow it in one chunk, but the routine below is use-
  43. # ful and good to have handy
  44. #
  45. while($read_size < $blob_size) {
  46.     $buffer .= $rs->Fields('logo')->GetChunk($bytes_to_read);
  47.     $read_size += $bytes_to_read;
  48.     if($read_size+$bytes_to_read > $ blob_size) {
  49.         $bytes_to_read = $blob_size - $read_size;
  50.     }
  51. }
  52.  
  53. # Make a VT_UI1 variant of the retrieved Chunks
  54. #
  55. $image = new Win32::OLE::Variant(VT_UI1, $buffer);
  56.  
  57. # Tell the browser that the content coming is an image of the type GIF
  58. #
  59. $Response->{ContentType}="image/gif";
  60.  
  61. # Do a binarywrite of the VT_UI1 variant image
  62. #
  63. $Response->BinaryWrite($image);
  64. %>