Skip to content Skip to sidebar Skip to footer

Download A File Using Javascript

There's this Excel file I want users to be able to download from my server. There must be an easy way to initiate the download of the file after a click on the 'Download' button...

Solution 1:

Actually, if you want a 'more-efficient' (and sexier) way, use:

location.href = your_url;

That way, you will save the compiler some time in going up to the location's prototype chain up to the window object.

Solution 2:

you're not going to believe this. Found it...

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

Sorry guys!

Solution 3:

If your server is configured to trigger a download for files of that mime type, it's as simple as this:

window.location = your_url

Solution 4:

Here's a VBScript function to download a binary file.

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 <> 200Then
    SaveUrlToFile = falseExitFunctionEndIf' 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
  EndIf
  stream.SaveToFile path

  SaveUrlToFile = trueEndFunction

Post a Comment for "Download A File Using Javascript"