1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

force download in ASP/WAP (urgent)

Discussion in 'C#' started by ludwig, May 14, 2006.

  1. #1
    HI everybody,

    I am trying to force download for images/audio files on my WAP page that uses ASP.

    I use this code for the site and it funcions well enough:

    
    					strPath=Server.MapPath(rsDownloads("file_name"))
    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 = file
    		'-- 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", "attachment; filename=" & objFile.Name
    		Response.AddHeader "Content-Length", objFile.Size
    		Response.ContentType = "application/octet-stream"
    		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("There is no such file.")
    		End If
    	Set objFSO = Nothing
    End Sub
    
    
    Code (markup):
    but on the WAP version it donwloads the wap.asp file which has this content.

    Please let me know what I am doing wrong.

    Thanks in advance
     
    ludwig, May 14, 2006 IP
  2. ludwig

    ludwig Notable Member

    Messages:
    2,253
    Likes Received:
    66
    Best Answers:
    0
    Trophy Points:
    225
    #2
    I think I have found out the problem, its the line

    Response.ContentType = "application/octet-stream"

    could you please write examples for JPG, GIF, JPEG, MID, MMF, MP3, THM file types.
     
    ludwig, May 15, 2006 IP
  3. ludwig

    ludwig Notable Member

    Messages:
    2,253
    Likes Received:
    66
    Best Answers:
    0
    Trophy Points:
    225
    #3
    I found what I needed. don't trouble guys,

    The list is at http://www.asptutorial.info/sscript/ContentType.asp

    I used a select case as I had several types there

    		select case right(objFile.Name, 3)
    			case "jpg", "peg"
    			Response.ContentType = "image/jpeg"
    			case "gif"
    			Response.ContentType = "image/gif"
    			case "mid", "midi"
    			Response.ContentType = "audio/midi"
    			case "mp3"
    			Response.ContentType = "audio/mpeg"
    			case "mmf"
    			Response.ContentType = "audio/mmf"
    			case "thm"
    			Response.ContentType = "theme/thm"
    		end select
    
    Code (markup):
     
    ludwig, May 16, 2006 IP