65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 COM 和 ASP 进行文件下载

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.91/5 (9投票s)

2000年9月6日

viewsIcon

293012

downloadIcon

4655

一个可用于 ASP 中传输文件的 COM 对象。

  • VC 组件源代码 - 43 Kb
  • VB 组件源代码 - 6 Kb
  • 示例 ASP 脚本 - 1 Kb
  • 引言

    本文介绍如何通过 COM 组件直接下载文件。它使用 IResponse 接口将文件直接发送到客户端浏览器。

    使用这种方法,您可以保护您的文件免受未经授权的人员下载。

    要将文件发送到客户端,您需要在 IIS MIME 类型中注册这种类型的文件。为此,请转到 IIS 管理器,并在 HTTP Header 下添加这种类型的文件。在示例中,使用了 zip 文件,因此 MIME 类型是

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

    要将文件发送到浏览器,您必须将 Response.Buffer 设置为 TRUE,并将 ContentType 设置为文件类型。之后,您可以使用 Response 对象通过 BinaryWrite 方法发送数据。

    为了完成此任务,开发了一个组件:FileTransfer。使用的接口称为 IBinaryRead。这些是它的方法

    方法 描述
    ReadBinFile(filename) 此方法将文件内容作为 VARIANT (SAFEARRAY) 返回
    ResponseBinaryWrite(filename, responseObject) 此方法读取文件并使用 Response 对象发送它。它返回操作结果

    如果您想下载自定义文件类型,您还必须在客户端机器上注册它。

    以下是使用 FileTransfer 对象的示例

    <% 
    	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
    %>
    
    © . All rights reserved.