2008-12-08 106 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