2010-12-20 35 views
0

要下載的文件(TXT)的代碼,我一直在嘗試2個diferent代碼,這是一個:錯誤在CLASIC ASP

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 

我發現它已經找到了非特定/定義的數據類型。

+0

你想下載一個txt或JPG? (標題與文件名不同) – 2010-12-20 22:26:44

+0

需要下載一個txt,我的錯誤 – 2010-12-21 12:42:41

回答

2

@Giancarlo索拉里諾:試試這個 -

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

在我的情況下,我必須重置流對象的位置,然後再讀取它:oStream.Position = 0(在While循環之前) – Dubs 2011-02-09 15:09:56

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 
+0

它運行良好,但事情是打開了一個下載文件的對話框,ot在瀏覽器中打開.txt。你有什麼想法我怎麼能得到一個對話框?謝謝。 – 2010-12-21 12:41:41

+0

假設'Response.AddHeader「Content-Disposition」,「attachment; filename =」&strFileName'強制瀏覽器顯示下載對話框,但它取決於瀏覽器和用戶配置。這是你能做的最好的事情。 – 2010-12-21 13:57:07