Figure 3    ImageDB

BOOL ReadFromFile(CComBSTR& bstrData, LPCWSTR pszFilePath)
{
    HANDLE hFile, hFileMapping;

    hFile = CreateFile(pszFilePath, GENERIC_READ, 0, NULL, OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL, NULL);

    if (hFile == INVALID_HANDLE_VALUE)
    return FALSE;

    DWORD dwSize = GetFileSize(hFile, NULL);

    hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
    BYTE* pbFile = (PBYTE) MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);

    BSTR bits = ::SysAllocStringLen(NULL, (int) ((dwSize + 1) / 2));
    memcpy(bits, pbFile, dwSize);

    UnmapViewOfFile(pbFile);
    CloseHandle(hFile);
    CloseHandle(hFileMapping);

    bstrData.Attach(bits);
    return TRUE;
}

STDMETHODIMP CImageDB::SetImage(VARIANT ADOField, BSTR bstrFilePath)
{
    
CComBSTR bstrImage;    // exception friendly BSTR
    try {
    if (ReadFromFile(bstrImage, (LPCWSTR) bstrFilePath))
    {
        // See the 
        VARIANT var;
        VariantInit(&var);
        V_VT(&var) = VT_BSTR;
        V_BSTR(&var) = bstrImage;

        FieldPtr field ((_variant_t) ADOField);
        if (field->Attributes & adFldLong)
            field->AppendChunk(var);
        else
            field->Value = var;
    }
    else
        return S_FALSE;
    } 
    catch(_com_error &e) { 
        ATLTRACE(L"Exception caught: %s\n", e.ErrorMessage());   
        return E_FAIL;
    }
    return S_OK;
}

STDMETHODIMP CImageDB::DeleteFile(BSTR bstrFilePath)
{
    ::DeleteFile(bstrFilePath);
    return S_OK;
}


Figure 4   TestImageDB.vbs


 ' Windows Scripting Host file to test ImageDB
 '
 set imagedb = WScript.CreateObject("ImageDB.ImageDB.1")
 Set rs = WScript.CreateObject("adodb.recordset")
 
 dsn = "DRIVER=SQL Server;SERVER=orca;UID=sa;PWD=;DATABASE=Mind"
 rs.Open "pictures", dsn, 2, 3
 rs.AddNew
 
 rs("Description") = "Test Picture"
 imagedb.SetImage rs.Fields("Bitmap"), "c:\temp\vs4.gif"
 
 ‘remove the comment from the next line to test DeleteFile
 ‘imagedb.DeleteFile "c:\temp\vs4.gif"
 
 WScript.Echo "Bitmap is " & rs("Bitmap").ActualSize & " bytes."
 set imagedb = nothing
 rs.Update
 rs.Close

Figure 6   Styles.css


 body  { background: steelblue; color: black; font-family: Verdana, Arial; 
         font-size: 8pt}
 table { font-size: 8pt; line-height: 12pt }
 
 span.BulletNumber { font-size: large; font-weight: bold; color: #CCCC99; 
                     line-height:14pt }
 span.BulletText   { font-size: x-small; font-weight: bold; text-align:center}
 
 a {text-decoration:none;}
 a:link { color: lightblue}
 a:visited {color: lightblue}
 a:active {color:yellow}

Figure 7   Default.asp


 <%@ LANGUAGE="VBSCRIPT" %>
 
 <HTML>
 <HEAD>
 <META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
 <TITLE>Document Title</TITLE>
 <LINK rel="stylesheet" href="styles.css">
 </HEAD>
 
 <BODY>
 <%
 Dim c, rs, thumbnails
 set thumbnails = Request("thumbnails") 
 
 Set c = Server.CreateObject("ADODB.Connection")
 c.Open Application("Mind_ConnectionString")
 if thumbnails = 1 then
     Set rs = c.Execute("select PictureID, Description, bitmap from Pictures")
 else    
     ‘ the following statement should be on a single line
     Set rs = c.Execute("select PictureID, Description, datalength(bitmap) as bytes from Pictures")    
 end if
 %>
 
 <table>
 <tr>
 
 <td valign=top>
 <span class=BulletNumber>Current<br>Pictures</span><br><br>
 <a href="AddImage.asp">&nbsp[Add a picture]</a><br>
 <%if thumbnails = 1 then%>
 <a href="default.asp?thumbnails=0">&nbsp[Hide thumbnails]</a><br>
 <%else%>
 <a href="default.asp?thumbnails=1">&nbsp[Show thumbnails]</a><br>
 <%end if%>
 <td align=top>
 <table>
 <tr style="font-weight:bold">
     <td>PictureID<td>Description
     <%if thumbnails then%><td>Thumbnail<%else%><td align=right>Bytes<%end if%>
 </tr>
 <%
 Do while (Not rs.eof) %>
     <tr>
         <td align=center><%=rs("PictureID")%>
         <td><a href="Picture.asp?PictureID=<%=rs("PictureID")%>">
             <%=rs("Description")%></a>
 <%if thumbnails = 1 then %>        
         <td><img height=50 src="Picture.asp?PictureID=<%=rs("PictureID")%>">
 <%else%>
         <td align=right><%=rs("bytes")%>
 <%end if%>
     </tr>
 <%  
     rs.MoveNext 
 Loop 
 %>
 </table>
 </td>
 </tr>
 </table>
 
 <%
 rs.close
  c.close 
 %>
 </BODY>
 </HTML>

Figure 10   Picture.asp


 <%@ LANGUAGE="VBSCRIPT" %>
 
 <%
 if request("PictureID") = "" then Response.End
 
 ' Clear out the existing HTTP header information
 Response.Expires = 0
 Response.Buffer = TRUE
 Response.Clear
 
 ' Change the HTTP header to reflect that an image is being passed.
 Response.ContentType = "image/gif"
 
 query = "select bitmap from pictures where pictureid = " & Request("PictureID") 
 set rsBlob = Server.CreateObject("adodb.recordset")
 rsBlob.Open query, Application("Mind_ConnectionString"), 2, 3
 
 if rsBlob.eof then Response.End
 if isnull(rsBlob(0)) then Response.End
 
 Response.BinaryWrite rsBlob(0)
 rsBlob.Close
 Set rsBlob = Nothing 
 Response.End
 %>

Figure 11   AddImage.asp


 <%@ LANGUAGE="VBSCRIPT"%>
 <HTML>
 <HEAD>
 <META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
 <TITLE>Add Image</TITLE>
 </HEAD>
 <LINK rel="stylesheet" href="styles.css">
 
 <body style="background:steelblue">
 
 <span class=BulletText>
 Add an image to the database.
 </span>
 
 <%
 action = "http://" & Request.ServerVariables("SERVER_NAME")
 action = action & "/scripts/cpshost.dll?PUBLISH?http://"
 action = action & Request.ServerVariables("SERVER_NAME") 
 action = action & "/mind/AddImageAction.asp"
 upload = "http://" & Request.ServerVariables("SERVER_NAME") & "/upload"
 %>
 
 <form enctype="multipart/form-data" 
     action=<%=action%>
     method=post name=form2>
 
 <table>
 
 <tr>
     <td><span class=BulletNumber>1.</span>Choose a file:</td>
     <td><input type="file" name=file1></td>
 </tr>
 <tr>
     <td><span class=BulletNumber>2.</span>Type a description:</td>
     <td><input type="text" name=description></td>
 </tr>
 <tr>
     <td><span class=BulletNumber>3.</span>Press this button:</td>
     <td><input type=submit value="Submit"></td>
 </tr>
 </table>
 <input type=hidden name="TargetURL" value=<%=upload%>>
 </form>
 
 </BODY>
 </HTML>

Figure 12   AddImageAction.asp


 <%@ LANGUAGE="VBSCRIPT" %>
 
 <%
 dim path
 if request.form("FilePath").count = 0 then
     Response.Redirect("AddImage.asp")
 else
     path = Request.Form("FilePath") & Request.Form("FileName") 
     path = path & Request.Form("FileExtention")
 end if
 %>
 
 <HTML>
 <HEAD>
 <TITLE>Add Image Status</TITLE>
 </HEAD>
 <LINK rel="stylesheet" href="styles.css">
 <BODY>
 
 <%
 
 ' Use "Mind.ImageDB.1" if you compiled the object with VC 6
 set imagedb = Server.CreateObject("ImageDB.ImageDB.1")
 
 Set rs = Server.CreateObject("adodb.recordset")
 rs.Open "pictures", Application("Mind_ConnectionString"), 2, 3
 rs.AddNew
 
 rs.Fields("Description") = Request.Form("description")
 imagedb.SetImage rs.Fields("Bitmap"), path
 imagedb.DeleteFile path
 
 rs.Update
 rs.Close
 set rs=nothing
 set foo = nothing
 
 %>
 
 The file '<%=Request.Form("FileName") & Request.Form("FileExtention")%>' with 
 description '<%=Request.Form("description")%>'
 has been added to the database.<br>
 
 <a href="/mind">[Back to home]</a>
 
 </BODY>
 </HTML>