Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.1 KiB

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