2008-12-08 105 views
9

我希望用戶能夠從我的服務器上下載這個Excel文件。點擊「下載」按鈕後,必須有一種簡單的方法來啓動文件的下載......但我不知道如何做到這一點。使用Javascript下載文件

我有這個迄今爲止:(VBScript和ASP)

<head> 
<script type="text/javascript" src="overzicht.js"></script> 
</head> 

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

    if (fs.FileExists("c:\file.xls"))=true then 'fake filename D: 
     response.write("<input type='button' value='Download Masterfile' class='button' onclick='exportmasterfile();' /><br />") 
    else 
     response.write("Masterfile not found. <br />") 
    end if 

    set fs=nothing 

JavaScript函數是空的。

+2

使用「添加評論」鏈接編寫您的評論,而不是寫新的答案... 1)用戶將被通知; 2)你不會混合評論和真實的解決方案。 – PhiLho 2008-12-08 13:40:31

+0

我們收到這些通知?在我查看我的個人資料後,我「不小心」遇到了您的評論,但沒有冒犯。謝謝:) – Kablam 2008-12-09 12:03:35

回答

19

你不會相信這一點。 找到它...

function exportmasterfile() 
{ var url='../documenten/Master-File.xls';  
    window.open(url,'Download'); 
} 

對不起傢伙!

5

如果您的服務器配置爲觸發下載該MIME類型的文件,它是像這樣簡單:

window.location = your_url 
21

其實,如果你想要一個「更有效的」(與性感)的方式,使用方法:

location.href = your_url; 

這樣的話,你會一段時間保存編譯器馬上要到location的原型鏈到window對象。

1

下面是一個用於下載二進制文件的VBScript函數。

Function SaveUrlToFile(url, path) 
    Dim xmlhttp, stream, fso 

    ' Request the file from the internet. 
    Set xmlhttp = CreateObject("MSXML2.XMLHTTP") 
    xmlhttp.open "GET", url, false 
    xmlhttp.send 
    If xmlhttp.status <> 200 Then 
    SaveUrlToFile = false 
    Exit Function 
    End If 

    ' Download the file into memory. 
    Set stream = CreateObject("ADODB.Stream") 
    stream.Open 
    stream.Type = 1 ' adTypeBinary 
    stream.Write xmlhttp.responseBody 
    stream.Position = 0 ' rewind stream 

    ' Save from memory to physical file. 
    Set fso = Createobject("Scripting.FileSystemObject") 
    If fso.Fileexists(path) Then 
    fso.DeleteFile path 
    End If 
    stream.SaveToFile path 

    SaveUrlToFile = true 
End Function