Calling Image from ASP Page / QueryString

Discussion in 'C#' started by mrbrantley, Jan 15, 2009.

  1. #1
    Got some images I am spreading around a few partner sites, but I need to track how many times they are loaded. I already have the counter and what not taken care of, im just stuck on getting the image I need to display on their site.

    Basically, they have a link like http://www.mysite.com/user.asp?id=100. When that page is called, it does its magic as to counting but I cannot get the picture to show on the partner sites. I just get a red X.

    Ive tried Resp.Redir, forcing the image through Binary writes (I think I did it right), everything I could find. Anyone have any feedback as to how this is done?

    Thanks!
     
    mrbrantley, Jan 15, 2009 IP
  2. mrbrantley

    mrbrantley Member

    Messages:
    87
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    Actually got it to work. Incase anyone else stumbles across this, its done with the following. Notice the content types. Change images/png to application if you want to force a download. The script kinda works 2 ways.

    <% Response.Buffer = True %>
    <%
    On Error Resume Next
    Dim strPath
    strPath = Server.MapPath("../your/directory/image.jpg")
    '-- do some basic error checking for the QueryString
    If strPath = "" Then
    	Response.Clear
    	Response.Write("No file specified.")
    	Response.End
    ElseIf InStr(strPath, "..") > 0 Then
    	Response.Clear
    	Response.Write("Illegal folder location.")
    	Response.End
    ElseIf Len(strPath) > 1024 Then
    	Response.Clear
    	Response.Write("Folder path too long.")
    	Response.End
    Else
    	Call DownloadFile(strPath)
    End If
    
    Private Sub DownloadFile(file)
    	'--declare variables
    	Dim strAbsFile
    	Dim strFileExtension
    	Dim objFSO
    	Dim objFile
    	Dim objStream
    	'-- set absolute file location
    	strAbsFile = Server.MapPath("../your/directory/image.jpg")
    	'-- create FSO object to check if file exists and get properties
    	Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    	'-- check to see if the file exists
    	If objFSO.FileExists(strAbsFile) Then
    		Set objFile = objFSO.GetFile(strAbsFile)
    		'-- first clear the response, and then set the appropriate headers
    		Response.Clear
    		'-- the filename you give it will be the one that is shown
    		' to the users by default when they save
    		Response.AddHeader "Content-Disposition", "inline; filename=" & objFile.Name
    		Response.AddHeader "Content-Length", objFile.Size
    		Response.ContentType = "images/png"
    		Set objStream = Server.CreateObject("ADODB.Stream")
    		objStream.Open
    		'-- set as binary
    		objStream.Type = 1
    		Response.CharSet = "UTF-8"
    		'-- load into the stream the file
    		objStream.LoadFromFile(strAbsFile)
    		'-- send the stream in the response
    		Response.BinaryWrite(objStream.Read)
    		objStream.Close
    		Set objStream = Nothing
    		Set objFile = Nothing
    	Else 'objFSO.FileExists(strAbsFile)
    		Response.Clear
    		Response.Write("No such file exists.")
    	End If
    	Set objFSO = Nothing
    End Sub
    %>
    Code (markup):
     
    mrbrantley, Jan 15, 2009 IP