Javascript: Force Download?

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?

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.

yeah thats kinda what I thought…oh well

You can use server-side script to do that.

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).

Rev

*Originally posted by claudio *
**You can use server-side script to do that. **

yes, you can :slight_smile:


<?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>

my server doesn’t support PHP, nor does my boss want it to (no idea why). Can this be done with ASP?

yes it can.

I don’t know ASP though… you’ll have to wait for Abzoid or someone to help you there :slight_smile:


  '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