Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_web_howto thread: Re: Record users downloads.


Message #1 by "George Draper" <gdraper@c...> on Fri, 22 Nov 2002 13:08:35 -0500
Hope this isn't too late.  I have tried the following code and it works.
 Remember to use Server.MapPath to specify the correct directory for the
location of the image files.  I got this from another mail list.  The
author's credits are at the end:

MASTER A FEW TRICKS OF THE TRADE

A trick that I see being used less and less frequently is transferring
data back to the server using the <IMG> tag. I saw it used a lot when
the CGI world was pretty much ruled by scripting languages such as Perl,
and it's still used in ads. However, with more sophisticated means of
relaying information, it becomes less necessary to incorporate certain
"tricks."

Sometimes you need to resort to good, old-fashioned tricks, such as
when your audience's capabilities are limited or their security is tight
as a drum. For instance, say you want to provide random images, but your
audience may not support JavaScript. Or, you're sending an
HTML-formatted e-mail, and you want to provide a "read receipt" for that
e-mail as long as the e-mail client supports HTML-formatted e-mail
messages. This can be done with the standard HTML <IMG> tag.

In order to accomplish this task in ASP, you should set the SRC
attribute of the <IMG> tag as this: mypage.asp. If you want to send
information back to the server, you could add a query string to the end
of your page call: mypage.asp?infotosend.

Serving up images in ASP requires the help of a DLL because you need to
open an image file for binary access. Within your ASP page, you would
change the ContentType property of the Response object to "image/jpeg"
for JPEG images, etc. You also need to add the Content-Length header to
whatever the byte length of the image is. Then the only thing left is to
send the image information through the BinaryWrite method of the
Response object.

You'll need to create an ActiveX DLL project within Visual Basic. Add
the following code to your class module:

Public Function GetImageFile(ByVal sFileName As Variant, _
    ByRef ImageData As Variant, ByRef DataLength As Variant) As
Boolean

    On Error GoTo ErrHandler
    Dim bData() As Byte
    Dim lFilSize As Long
        
    Open sFileName For Binary Access Read As #1
    
    lFilSize = LOF(1)
    
    ReDim bData(lFilSize) As Byte

    Dim i As Integer
    i = 0
    While i <= lFilSize
        Get #1, i + 1, bData(i)
        i = i + 1
    Wend
    
    ImageData = bData()
    DataLength = lFilSize

    GetImageFile = True
    
ExitCall:
    Close #1
Exit Function
ErrHandler:
    'Log error here.
    GoTo ExitCall
End Function

For this example, I kept the VB project name as "Project1" and the
class name as "Class1," although I suggest using a more appropriate
naming scheme. To serve this data up in your ASP page (using a query
string as the file name), use this code:

<%
Response.Buffer = False
Response.Expires = -1

Dim objImg
Dim lImgLength, vImgData

Set objImg = Server.CreateObject("Project1.Class1")

objImg.GetImageFile Request.QueryString, vImgData, lImgLength

Response.ContentType = "image/jpeg"
Response.AddHeader "Content-Length", lImgLength

Response.BinaryWrite vImgData

Response.End
%>

Modifying this code will allow you to be able to serve up images from
any source including blob fields in a database. You would also need to
identify the image type when requested. This could be done by
recognizing the image extension name, but it would be more appropriate
to read the file header within the image file. This method could also be
used to create a central access point for all your images; thus,
allowing for other checks to be performed before serving up the image. A
"read receipt" could be created since once an HTML rendering engine
encounters an <IMG> tag, the client has to request that file from the
server again. This occurs when the e-mail client opens the
message--which is when the message is "read."

You can achieve simple HTTP tricks once you change the appropriate
headers and serve up data through BinaryWrite--as long as you remain
within the confines of the HTTP standards. It's important to remember
that HTTP requests are usually stateless, so HTTP expects a mere
connection open, a request, a response, and a connection close. If the
client understands the MIME type, you can provide all kinds of files in
this fashion.

Phillip Perkins is a contractor with Ajilon Consulting. His experience
ranges from machine control and client/server to corporate intranet
applications.

----------------------------------------


Good luck!

- George

>>> charliederees@h... 10/17/2002 7:03:57 AM >>>
I would like to create a site where by users can download images. Each

time a users downloads I would like to record a count of how many time

they download an image.

I have my database setup with an account table and a table to to store

AccountID, image ID and count but I would like to know how to code a
page 
to write to the count field.

Can anyone help ?

Charlie

---

Improve your web design skills with these new books from Glasshaus.

Usable Web Menus
http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=nosim/theprogramme

r-20
Constructing Accessible Web Sites
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=nosim/theprogramme

r-20
Practical JavaScript for the Usable Web
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=nosim/theprogramme

r-20

  Return to Index