views:

27955

answers:

7

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... but I have no clue how to make that happen.

I have this so far: (VBscript and 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

The javascript function is empty.

+5  A: 

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

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

Sorry guys!

Skunk
Why do you say you're sorry? And if this is the solution you used, why isn't it marked as such?
mizipzor
+4  A: 

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
Nick Retallack
+9  A: 

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.

Andreas Grech
That's not sexy.
eyelidlessness
I find it quite charming actually
Andreas Grech
Like most of the things I remember from the mid-90s, I'd probably want to put a paper bag over its head. Personally.
eyelidlessness
nice, that is sexy, and I should know that.
craigmoliver
A: 

So that's a bit faster then the earlier solution?

(I can't vote up yet, sorry)

Skunk
Kiki, please note that SO is not a forum or NG. Use add comment feature for this sort of thing. Answers should be Answers to the question. The set of answers do not form a conversation and will generally be read in order of most votes not time posted.
AnthonyWJones
Yes. Someone told me that already. In the first post in this question actually, for all to see. Please realize that I am brand new to this site and community, and still learning how it all works. It's not exactly my fault that there is noting in the FAQ about this. Trial and error.
Skunk
A: 

sorry bros. But this will not work if your file type is '.txt'. It is getting opened on browser tab. :( Is there any way to make a TXT file down loadable.

Maulik Vora
you need to set the Content-type: header from the server to something which the browser will not attempt to display, eg "Content-type: application/force-download" ... if the browser is configured to display XLS inline, it will do that when you click on an .xls file too.
Chris Burgess
A: 

Good Answers. I see the use for the code above; but how would you parse the contents of the retrieved file (if it is a CSV file for example) into a javascript variable without ever leaving the current html page.

A: 

free javascript dowlod

naresh