Flash: Force Download
June 1st, 2005 by LuisThe simplest solution to force a download of an image directly from flash. I haven't tested it in all browsers, but it works in explorer.
Sample swf: (just right click the picture to download the image):
[flash]http://www.blog.lessrain.com/wp-content/upload/test.swf,400,200[/flash]
There is nothing new here, basically the trick is a simple getURL call to a php script that forces the picture to download: AS code:
Actionscript:
- var myPic:String='IMG_0200.jpg';
- var my_cm:ContextMenu = new ContextMenu();
- var menuItem_cmi:ContextMenuItem = new ContextMenuItem("Download Picture", down);
- my_cm.customItems.push(menuItem_cmi);
- my_cm.hideBuiltInItems();
- IMG_0200.menu = my_cm;
- function down() {
- getURL ("swfsamples/forcedownload.php?file="+myPic);
- }
PHP code:
PHP:
- $filename = $_GET['file'];
- // required for IE, otherwise Content-disposition is ignored
- ?>


June 1st, 2005 at 6:40 pm
Does it violate any of the terms of viewing a website?
June 1st, 2005 at 7:01 pm
What do you mean with violate any of terms of viewing a website?
June 3rd, 2005 at 3:22 pm
nice work luis
June 3rd, 2005 at 3:25 pm
[…] ome code to enable you to right click and down load a picture (jpg etc) from a swf file seeForce Picture Download for more information Permalink Leave a reply […]
June 3rd, 2005 at 5:37 pm
works in firefox as well
well done Luis
hey mate, I still owe you a beer. Just tell me the place and the date
salut
June 3rd, 2005 at 7:52 pm
if you leave the script this way somebody could easily download sensitive information from the server like php sourcefiles with passwords etc.!
so you should always add some limitations to the script. in this case i just added the line:
if ($filename != “IMG_0200.jpg”) exit();
you can limit it to filetypes too, but directory access limits should really always be there…
June 3rd, 2005 at 8:08 pm
using the following method you can compare the directory-name relative to the base-url of the website:
$baseurl = strreplace($DOCUMENT_ROOT, “”, dirname($filename));
in our case would return “/swfsamples”
so you could say:
$fileextension = strtolower(substr(strrchr($filename,”.”),1));
if ($baseurl != “/swfsamples†|| $file_extension!=”jpg”) exit();
a combined directory/mimetype check!
June 3rd, 2005 at 8:44 pm
Actually, there are some interesting comments about the use of the header function in php.net.
http://uk2.php.net/header
Bruno, whenever you want, we can have that beer
September 16th, 2005 at 12:52 pm
[…] ore dirty tricks in Flash 8 September 16th, 2005 by Luis Following up our post Force Download where we discussed a few tricks to download a picture directly from flash using getURL and a s […]
January 7th, 2006 at 9:51 am
I am forcing a download of image using this script.
In flash i have just used : geturl()
and passing this to php.
Issue: now this works fine with IE, but in Firefox it is downloading the pic but only after removing its extension. and thus one has to add the extension manually for file to open
March 19th, 2006 at 2:44 am
@ Harish
Did you leave out the spaces? Firefox got a problem with spaces in a filename…
October 8th, 2006 at 2:20 pm
mikaël, try this:
import flash.net.FileReference;
var myMP3= “http://www.yourdomain.com/mp3/mymp3.mp3″;
var fileRef:FileReference = new FileReference();
function bringmetheMP3(){
fileRef.download(myMP3)
}
February 22nd, 2007 at 12:27 am
So the code works great on your example but where is it applied? on the first frame of the scene or on the picture or button that is clickable?
February 22nd, 2007 at 2:47 am
I know that your demo works but I’m not sure how to apply the code to a swf file. where does the php code go and where does the actionscript go. I am trying to make it possible to download mp3’s.
June 21st, 2007 at 10:38 pm
Great bit of coding.
Works well in firefox and opera, however i cannot get the content-disposition code at the top of the php to enable the filename to be carried across in internet explorer. It just defaults to save a file ‘forcedownload.jpg’.
Any ideas on how to sort this?
Cheers Luis
August 23rd, 2007 at 12:42 am
excellent demo, is there anyway to apply it to a button/link in flash that forces a pdf to download?
April 14th, 2009 at 12:10 am
[…] http://www.blog.lessrain.com/flash-force-download/ […]