【问题标题】:Download a file using Javascript使用 Javascript 下载文件
【发布时间】:2010-09-25 19:57:30
【问题描述】:

我希望用户能够从我的服务器下载这个 Excel 文件。单击“下载”按钮后,必须有一种简单的方法来启动文件的下载......但我不知道如何做到这一点。

到目前为止我有这个:(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函数为空。

【问题讨论】:

  • 使用“添加评论”链接写你的 cmets,而不是写新的答案...... 1)用户将被通知; 2)你不会把言论和真正的解决方案混为一谈。
  • 我们收到这些通知?在我检查了我的个人资料后,我“不小心”看到了你的评论,没有冒犯。谢谢:)

标签: javascript asp-classic vbscript


【解决方案1】:

你不会相信的。 找到了...

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

对不起各位!

【讨论】:

  • 你为什么说你很抱歉?如果这是您使用的解决方案,为什么没有这样标记?
【解决方案2】:

如果您的服务器配置为触发下载该 mime 类型的文件,那么就这么简单:

window.location = your_url

【讨论】:

    【解决方案3】:

    实际上,如果您想要一种“更高效”(更性感)的方式,请使用:

    location.href = your_url;
    

    这样,您将节省编译器一些时间,使您可以将 location 的原型链向上延伸到 window 对象。

    【讨论】:

      【解决方案4】:

      这是一个下载二进制文件的 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
      

      【讨论】:

        猜你喜欢
        • 2021-12-18
        • 2015-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-22
        相关资源
        最近更新 更多