Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

File downloading with COM and ASP

0.00/5 (No votes)
5 Sep 2000 1  
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:

    Method Description
    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