Click here to Skip to main content
15,886,199 members
Articles / Web Development / IIS
Article

File downloading with COM and ASP

,
Rate me:
Please Sign up or sign in to vote.
4.91/5 (11 votes)
5 Sep 2000 287.1K   4.7K   50   59
A COM object you can use to transfer files in ASP
  • VC Component source - 43 Kb
  • VB Component source - 6 Kb
  • Example ASP script - 1 Kb
  • Introduction

    This article is about direct file downloading through a COM component. It uses the IResponse interface to send a file directly to the client browser.

    With this approach you can protect your files from being downloaded by an unauthorized person.

    To send the file to the client you need to register this kind of file within the IIS MIME types. To do it go to the ISS administration and under the HTTP Header add this kind of file. In the sample a zip file is used, so the MIME type is:

    Extension=.zip
    ContentType=application/x-zip-compressed

    To send a file to the browser you must set the Response.Buffer to TRUE and the ContentType to the file type. After this you send the data using the Response object through the BinaryWrite method.

    To accomplish this task a component was developed: FileTransfer. The interface used is called IBinaryRead. These are its methods:

    MethodDescription
    ReadBinFile(filename) This method returns the content of the file as an VARIANT (SAFEARRAY)
    ResponseBinaryWrite(filename, responseObject) This method reads the file and send it using the Response object. It returns the operation result

    If you want to download a custom file type, you must register it in the client machine too.

    An example of using the FileTransfer object follows:

    <% 
    	Option Explicit
    
    	Dim objBinaryRead 
    	Dim saBinFile		'if you don't want error safe
    	Dim bDownloadStatus
    	
    	Set objBinaryRead = CreateObject("FileTransfer.BinaryRead")
    		 
    	Response.Buffer      = true
    	Response.ContentType = "application/x-zip-compressed"
    
    	'transfer the file to the client		
    	bDownloadStatus = objBinaryRead.ResponseBinaryWrite("c:\temp\test.zip", Response)
    	
    	'this is not an error safe code
    	'if the file doesn't exist the ASP will have an error
    	'saBinFile = objBinaryRead.ReadBinFile("c:\temp\test.zip")     
    	'Response.BinaryWrite saBinFile
    	'Set saBinFile     = nothing
    	
    	Set objBinaryRead = nothing
    %>

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Architect VisionOne AG
    Switzerland Switzerland
    XicoLoko is a brazilian developer based in Switzerland.

    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralAccess to Excel problem Pin
    SimonGoldsmith5-Dec-02 0:44
    SimonGoldsmith5-Dec-02 0:44 
    QuestionHow to know where the file is saved ? Pin
    Anonymous19-Nov-02 15:34
    Anonymous19-Nov-02 15:34 
    AnswerRe: How to know where the file is saved ? Pin
    Anonymous15-Jul-05 18:35
    Anonymous15-Jul-05 18:35 
    Generalthere much time for large file Pin
    jinux23-Sep-02 19:29
    jinux23-Sep-02 19:29 
    Generaldowloading a pdf file Pin
    Arti17-Jun-02 6:26
    Arti17-Jun-02 6:26 
    GeneralRe: dowloading a pdf file Pin
    23-Jun-02 14:29
    suss23-Jun-02 14:29 
    GeneralRe: dowloading a pdf file Pin
    Anonymous30-Jan-04 9:26
    Anonymous30-Jan-04 9:26 
    Generaldownloading files with Netscape Pin
    3-Jan-02 0:24
    suss3-Jan-02 0:24 
    GeneralFile Download Pin
    15-Nov-01 12:25
    suss15-Nov-01 12:25 
    GeneralDownload file Pin
    7-Nov-01 1:39
    suss7-Nov-01 1:39 
    Generalprocess names the file incorrectly Pin
    lyndonjohnson14-Aug-01 9:56
    lyndonjohnson14-Aug-01 9:56 
    GeneralRe: process names the file incorrectly Pin
    Tai15-Aug-01 16:23
    Tai15-Aug-01 16:23 
    GeneralRe: process names the file incorrectly Pin
    6-Sep-01 5:15
    suss6-Sep-01 5:15 
    Generalmime types Pin
    2-Aug-01 3:49
    suss2-Aug-01 3:49 
    GeneralRe: mime types Pin
    6-Aug-01 2:34
    suss6-Aug-01 2:34 
    QuestionHow to use? Pin
    31-Jul-01 22:22
    suss31-Jul-01 22:22 
    AnswerRe: How to use? Pin
    EZUID16-Jan-03 18:49
    EZUID16-Jan-03 18:49 
    Questiona zip file with ASP?? Pin
    31-Jul-01 6:43
    suss31-Jul-01 6:43 
    Generalchoosing which file is downloaded Pin
    26-Jul-01 11:29
    suss26-Jul-01 11:29 
    GeneralRe: choosing which file is downloaded Pin
    13-Mar-02 2:27
    suss13-Mar-02 2:27 
    GeneralRe: choosing which file is downloaded Pin
    xicoloko13-Mar-02 5:14
    xicoloko13-Mar-02 5:14 
    QuestionHow to find if the file was successfully downloaded? Pin
    9-May-01 15:46
    suss9-May-01 15:46 
    AnswerRe: How to find if the file was successfully downloaded? Pin
    14-Jun-01 4:16
    suss14-Jun-01 4:16 
    QuestionHow set File Download dialog Pin
    5-Apr-01 5:02
    suss5-Apr-01 5:02 
    AnswerRe: How set File Download dialog Pin
    14-Jun-01 4:13
    suss14-Jun-01 4:13 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.