在经典ASP中下载文件(txt)的代码出错

时间:2010-12-20 19:29:51

标签: file asp-classic download

我一直在尝试两种不同的代码:

nombre="Prueba.txt"
set stream = Server.CreateObject("ADODB.Stream")
stream.Open
stream.Type = 2 ' binary
stream.LoadFromFile(Server.MapPath("./File/"&nombre))
Response.BinaryWrite(stream.Read)

我正在尝试的其他代码是:

Response.ContentType = "application/x-unknown" ' arbitrary 

FPath = Server.MapPath("./File/"&nombre) 
Response.AddHeader "Content-Disposition","attachment; filename=" & nombre 

Set adoStream = CreateObject("ADODB.Stream") 
adoStream.Open() 
adoStream.Type = 1 
adoStream.LoadFromFile(FPath) 
Response.BinaryWrite adoStream.Read() 
adoStream.Close 
Set adoStream = Nothing 

Response.End 

我发现它找到了一个非特定/定义的数据类型。

2 个答案:

答案 0 :(得分:2)

@Giancarlo Solarino:试试这个 -

Option Explicit

Dim sFileName, sFilePath, iFileSize
Dim oFile, oFS, oStream

sFileName = "Prueba.txt"
sFilePath = Server.MapPath("File/" & sFileName)

Set oFS = Server.CreateObject("Scripting.FileSystemObject")

If oFS.FileExists(sFilePath) Then
    Set oFile = oFS.GetFile(sFilePath)
    iFileSize = oFile.Size
    Set oFile = Nothing

    Response.AddHeader "Content-Disposition","attachment; filename=" & sFileName
    Response.ContentType = "application/download"
    Response.AddHeader "Content-Length", iFileSize

    Set oStream = Server.CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.LoadFromFile sFilePath

    Do While NOT oStream.EOS AND Response.IsClientConnected
        Response.BinaryWrite oStream.Read(1024)
        Response.Flush()
    Loop

    oStream.Close
    Set oStream = Nothing
End If

答案 1 :(得分:0)

我有一些正常工作的代码,但我最后使用ContentType = "image/jpeg"Response.Flush

Const adTypeBinary = 1

dim strFileName
strFileName = "archivo.jpg"

dim strFilePath
strFilePath = "C:\temp\" + strFileName

dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile strFilePath

Response.AddHeader "Content-Disposition", "attachment; filename=" & strFileName
Response.Charset = "UTF-8"
Response.ContentType = "image/jpeg"

Response.BinaryWrite objStream.Read
Response.Flush