downloadable files

Discussion in 'C#' started by red_fiesta, Nov 10, 2006.

  1. #1
    is there a way to make download files so that a user has to fill in a form first.

    for example i have a file which users may only have once they have filed in a form, on correct completion of the form they can download the file.

    The way i see it is once people know this link after they have completed the form they can get this file again and again as the download is shown as a link.

    How would i make this so that the location of this files is hidden?

    do this make sense?
     
    red_fiesta, Nov 10, 2006 IP
  2. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #2
    Just do a redirect to the file. Create your form fields, add your required validators, if all goes well - redirect to the downloadable file :)
     
    ccoonen, Nov 10, 2006 IP
  3. red_fiesta

    red_fiesta Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah but they would still know the link??

    u mean a response.redirect right?

    the idea is that they cant see the link or just type the link in to the file
     
    red_fiesta, Nov 11, 2006 IP
  4. Garve

    Garve Peon

    Messages:
    62
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Use the File System Object to change the name of the file or the folder it's in after it's been downloaded, or perhaps once a day if that fits your business model.

    cheers

    Garve
     
    Garve, Nov 11, 2006 IP
  5. ludwig

    ludwig Notable Member

    Messages:
    2,253
    Likes Received:
    66
    Best Answers:
    0
    Trophy Points:
    225
    #5
    here is an example which forces the download
    if you have problems with the code klet me know
     
    ludwig, Nov 12, 2006 IP
  6. red_fiesta

    red_fiesta Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    i used this code..

    first page

    <html>
    <head>
    <title>Download File Example</title>
    </head>
    <body>
    <a href="DownloadManager.asp?file=myfile.doc&force=false">Download file without forcing Save As box</a><br />
    <a href="DownloadManager.asp?file=myfile.doc&force=true">Download file (forcing Save As box)</a>
    </body>
    </html>
    Code (markup):

    downloadmanager.asp
    <% Option Explicit %>
    <% Response.Buffer=True %>
    <%
    'Constants
    Const FOLDER_PATH="c:\Secure" 'full path to the secure folder
    Const adTypeBinary = 1
    Const adTypeText = 2
    Const chunkSize = 2048
    
    Dim strFileName, force 'name of the file to be downloaded
    Dim iSz, i 'used to write in chunks
    strFileName=Request("file")
    'If Len(strFileName)>0 Then
    '	Call DownloadFile (strFileName, "true") 'Force download
    'End If
    
    If Len(strFileName)>0 Then
    	Call DownloadFile (strFileName, force) 'Force download
    End If
    
    Sub DownloadFile(strFileName, blnForceDownload)
    	Dim fso, objFile, strFilePath
    	Dim fileSize, blnBinary, strContentType
    	Dim objStream, strAllFile
    	
    	'----------------------
    	'first step: verify the file exists
    	'----------------------
    	
    	'build file path:
    	strFilePath=FOLDER_PATH
    	' add backslash if needed:
    	If Right(strFilePath, 1)<>"\" Then strFilePath=strFilePath&"\"
    	strFilePath=strFilePath&strFileName
    	
    	'initialize file system object:
    	Set fso=Server.CreateObject("Scripting.FileSystemObject")
    	
    	'check that the file exists:
    	If Not(fso.FileExists(strFilePath)) Then
    		Set fso=Nothing
    		Err.Raise 20000, "Download Manager", "Fatal Error: file does not exist: "&strFilePath
    		Response.END
    	End If
    	
    	'----------------------
    	'second step: get file size.
    	'----------------------
    	Set objFile=fso.GetFile(strFilePath)
    	fileSize=objFile.Size
    	Set objFile=Nothing
    	
    	'----------------------
    	'third step: check whether file is binary or not and get content type of the file. (according to its extension)
    	'----------------------
    	blnBinary=GetContentType(strFileName, strContentType)
    	strAllFile=""
    	
    	'----------------------
    	'forth step: read the file contents.
    	'----------------------
    	If blnBinary Then
    		Set objStream=Server.CreateObject("ADODB.Stream")
    		
    		'Added to breakup chunk
    		Response.Buffer = False 
    		
    		'this might be long...
    	    	Server.ScriptTimeout = 30000
    		
    		'send proper header:
    	    	Response.AddHeader "Content-Disposition", "attachment; filename="&strFileName
    		
    		'----------------------
    		objStream.Open
    		objStream.Type = 1 'adTypeBinary
    		objStream.LoadFromFile strFilePath
    		
    		'Added to breakup chunk
    		iSz = objStream.Size
    		Response.AddHeader "Content-Length", iSz
    		Response.Charset = "UTF-8"
    		Response.ContentType = strContentType
    		For i = 1 To iSz \ chunkSize
    			If Not Response.IsClientConnected Then Exit For
    			Response.BinaryWrite objStream.Read(chunkSize)
    		Next 
    		If iSz Mod chunkSize > 0 Then 
    			If Response.IsClientConnected Then 
    				Response.BinaryWrite objStream.Read(iSz Mod chunkSize)
    			End If 
    		End If
      		objStream.Close
    		Set objStream = Nothing		
    		'--------------------------------------
    		'Commented out Original Code
    		'strAllFile=objStream.Read(fileSize)
    		'objStream.Close
    		'Set objStream = Nothing
    		'--------------------------------------
    	Else  
    		Set objFile=fso.OpenTextFile(strFilePath,1) 'forReading
    		If Not(objFile.AtEndOfStream) Then
    			strAllFile=objFile.ReadAll
    		End If
    		objFile.Close
    		Set objFile=Nothing
    		Response.Write(strAllFile)
    	End If
    	
    	'----------------------
    	'final step: apply content type and send file contents to the browser
    	'----------------------
    	'Commented out Original Code
    	If blnForceDownload="true" Then
    		Response.AddHeader "Content-Disposition", "attachment; filename="&strFileName
    	End If
    	Response.AddHeader "Content-Length", fileSize
    	Response.Charset = "UTF-8"
    	Response.ContentType = strContentType
    	If blnBinary Then
    		Response.BinaryWrite(strAllFile)
    	Else  
    		Response.Write(strAllFile)
    	End If
    	'-----------------------------
    	
    	'clean up:
    	Set fso=Nothing
    	Response.Flush
    	Response.END
    End Sub
    
    Function GetContentType(ByVal strName, ByRef ContentType)
    	'return whether binary or not, put type into second parameter
    	Dim strExtension
    	strExtension="."&GetExtension(strName)
    	Select Case strExtension
    		Case ".asf"
    			ContentType = "video/x-ms-asf"
    			GetContentType=True
    		Case ".avi"
    			ContentType = "video/avi"
    			GetContentType=True
    		Case ".doc"
    			ContentType = "application/msword"
    			GetContentType=True
    		Case ".zip"
    			ContentType = "application/zip"
    			GetContentType=True
    		Case ".xls"
    			ContentType = "application/vnd.ms-excel"
    			GetContentType=True
    		Case ".gif"
    			ContentType = "image/gif"
    			GetContentType=True
    		Case ".jpg", ".jpeg"
    			ContentType = "image/jpeg"
    			GetContentType=True
    		Case ".wav"
    			ContentType = "audio/wav"
    			GetContentType=True
    		Case ".mp3"
    			ContentType = "audio/mpeg3"
    			GetContentType=True
    		Case ".wma" 
    			ContentType = "audio/wma"
    			GetContentType=True
    		Case ".mpg", ".mpeg"
    			ContentType = "video/mpeg"
    			GetContentType=True
    		Case ".pdf"
    			ContentType = "application/pdf"
    			GetContentType=True
    		Case ".rtf"
    			ContentType = "application/rtf"
    			GetContentType=True
    		Case ".htm", ".html"
    			ContentType = "text/html"
    			GetContentType=False
    		Case ".asp"
    			ContentType = "text/asp"
    			GetContentType=False
    		Case ".txt"
    			ContentType = "text/plain"
    			GetContentType=False
    		Case Else
    			'Handle All Other Files
    			ContentType = "application/octet-stream"
    			GetContentType=True
    	End Select
    End Function
    
    Function GetExtension(strName)
    	Dim arrTmp
    	arrTmp=Split(strName, ".")
    	GetExtension=arrTmp(UBound(arrTmp))
    End Function
    %>
    Code (markup):
    the problem i have is

    whether i use

    Download file without forcing Save As box

    or

    Download file (forcing Save As box)

    its brings up the save window and not the other option...
     
    red_fiesta, Nov 12, 2006 IP