# Javascript: Force Download?

**URL:** <https://forum.kirupa.com/t/javascript-force-download/42223>\
**Category:** web dev\
**Created:** [February 6, 2004, 4:37pm UTC](https://forum.kirupa.com/t/javascript-force-download/42223 "2004-02-06T16:37:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrand01](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@mrand01](https://forum.kirupa.com/u/mrand01)\
**Post date:** [February 6, 2004, 4:37pm UTC](https://forum.kirupa.com/t/javascript-force-download/42223/1 "2004-02-06T16:37:19Z")

</div>

I’m trying to make Javascript force a download, but I don’t know how. Here is my current function:

```auto

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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 6, 2004, 5:51pm UTC](https://forum.kirupa.com/t/javascript-force-download/42223/2 "2004-02-06T17:51:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 6, 2004, 5:58pm UTC](https://forum.kirupa.com/t/javascript-force-download/42223/3 "2004-02-06T17:58:07Z")

</div>

yeah thats kinda what I thought…oh well

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 6, 2004, 6:00pm UTC](https://forum.kirupa.com/t/javascript-force-download/42223/4 "2004-02-06T18:00:14Z")

</div>

You can use server-side script to do that.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 6, 2004, 6:01pm UTC](https://forum.kirupa.com/t/javascript-force-download/42223/5 "2004-02-06T18:01:24Z")

</div>

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 ![](http://aulman.com/rev.gif)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 6, 2004, 7:03pm UTC](https://forum.kirupa.com/t/javascript-force-download/42223/6 "2004-02-06T19:03:44Z")

</div>

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

yes, you can 🙂

```php

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

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

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 6, 2004, 8:50pm UTC](https://forum.kirupa.com/t/javascript-force-download/42223/7 "2004-02-06T20:50:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 6, 2004, 9:04pm UTC](https://forum.kirupa.com/t/javascript-force-download/42223/8 "2004-02-06T21:04:13Z")

</div>

yes it can.

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 6, 2004, 9:20pm UTC](https://forum.kirupa.com/t/javascript-force-download/42223/9 "2004-02-06T21:20:42Z")

</div>

```auto

  '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

```
