PDA

View Full Version : Javascript: Force Download?



mrand01
February 6th, 2004, 12:37 PM
I'm trying to make Javascript force a download, but I don't know how. Here is my current function:



function saveFile() {
var list = document.playerCtrl.downloads;
var downloadURL = list.options[list.selectedIndex].value;
window.location.href = downloadURL;
}


all of the downloadURLs are pointed to WMVs (videos) on a server. This (of course) automatically opens Windows Media Player. Is there any way for me to force a right click/save as?

blindlizard
February 6th, 2004, 01:51 PM
You cannot force a save as because there would be a major security issue with that. I don't know a browser out there that would allow that to happen.

mrand01
February 6th, 2004, 01:58 PM
yeah thats kinda what I thought...oh well

claudio
February 6th, 2004, 02:00 PM
You can use server-side script to do that.

reverendflash
February 6th, 2004, 02:01 PM
The only way, is to change the extension to something unique (.uwe for example).

then, you instruct the user to change the extension on saving to .wmv or whatever...

if the client has that extension assigned to something, it will open it by default, unless they have selected to be notified, and to choose each time they click a link (which can be a pain).

Revhttp://aulman.com/rev.gif

λ
February 6th, 2004, 03:03 PM
Originally posted by claudio
You can use server-side script to do that.

yes, you can :)



<?php
header("Content-Type:application/octet-stream");
header("Content-Disposition:attachment;");
$fName = basename($_GET['file']);
fpassthru($fName);
?>

Just save that file where you store the files on the server.

If all your movies and that file were in a folder called videos, and you wanted to download "random.wmv", then you'd do this:


<a href="videos/filedownload.php?file=random.wmv">Cool Video!</a>

mrand01
February 6th, 2004, 04:50 PM
my server doesn't support PHP, nor does my boss want it to (no idea why). Can this be done with ASP?

λ
February 6th, 2004, 05:04 PM
yes it can.

I don't know ASP though.. you'll have to wait for Abzoid or someone to help you there :)

blindlizard
February 6th, 2004, 05:20 PM
'Create a stream object
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")

'Open a Sound File
objStream.Type = adTypeBinary
objStream.Open
objStream.LoadFromFile "c:\sound.wmv"

'Output the contents of the stream object
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", "attachment;filename=""sound.wmv"" Response.BinaryWrite objStream.Read

'Clean up....
objStream.Close
Set objStream = Nothing