PDA

View Full Version : as3 to php download image to desktop


ljones
10-05-2011, 06:01 PM
Passing the path and filename to a php script, which I would like to download the image to the users box. I cannot get this to work properly, I have tried multiple examples with limited success. When I run the php script straight away it works as expected ( with the path and filename hard coded).
var request:URLRequest = new URLRequest (Settings.DOWNLOAD_IMAGE);
request.method = URLRequestMethod.POST;

var variables:URLVariables = new URLVariables();
var _path:String = img.substring(0,img.lastIndexOf("/") + 1);
var _image:String = img.substring(img.lastIndexOf("/") + 1);

variables.filePath = _path;
variables.fileName = _image;
request.data = variables;

var loader:URLLoader = new URLLoader (request);
loader.addEventListener(Event.COMPLETE, onComplete);
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(request);


$filePath = "http://mypath/"; // . $_POST['filePath'];
$fileName = "item1.jpg"; //$_POST['fileName'];

$source = $filePath . $fileName;

header('Content-disposition: attachment; filename=' .$fileName);
header('Content-type: application/jpg');
readfile($source);

Any help is greatly appreciated.

ljones
10-05-2011, 08:54 PM
this did the trick:

var request:URLRequest = new URLRequest(obj.imgPath + obj.downloadImg);
var localRef:FileReference = new FileReference();

try
{
// Prompt and downlod file
localRef.download( request );
}
catch (error:Error)
{
trace("Unable to download file.");
}