Código Flex :
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300"
creationComplete="init()" >
<fx:Script>
<![CDATA[
import flash.net.URLRequest;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
//import flash.net.FileReference;
private var imagesFilter:FileFilter = new FileFilter("Images(*.jpg; *.gif; *.png)", "*.jpg;*.gif;*.png");
private var photoShop:FileFilter = new FileFilter("Archivos Photoshop", "*.psd");
private var documents:FileFilter = new FileFilter("Documentos(*.docx; *.xlsx; *.ppt; *.txt)","*.docx; *.xlsx; *.ppt; *.txt");
private var comprimidos:FileFilter = new FileFilter("Comprimidos(*.rar; *.zip)","*.rar; *.zip");
private var todos:FileFilter = new FileFilter("Todos los Archivos(*.*)","*.*");
private var fileref:FileReference = new FileReference();
/** aqui busco los archivos de la carpeta */
private function buscar():void
{
fileref.browse([imagesFilter,photoShop,documents,comprimidos,todos]);
}
private function seleccionar(event:Event):void
{
startUpload.enabled = true;
descText.text = fileref.name;
}
private function init():void{
startUpload.enabled = true;
fileref.addEventListener(Event.SELECT, seleccionar);
// fileref.addEventListener(ProgressEvent.PROGRESS, progressHandler);
// fileref.addEventListener(Event.COMPLETE, completeHandler);
}
/** mando llamar la url donde esta mi clase en php para realizar la descarga*/
protected function startUpload_clickHandler(event:MouseEvent):void
{
// mirem.Consulta_Empleado_Kardez(TxtRPE.text.toString());
//descText.text=url.toString();
var url :URLRequest = new URLRequest("http://localhost/flex/clasesp1/Download/src/amfphp/services/Clases/descarga.php?file=" + descText.text);
navigateToURL(url, "_blank")
descText.text="";
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Panel x="75" y="57" width="250" height="200" title="Descargar Archivo Control de Gestion">
<s:TextInput x="28" y="55" id="descText"/>
<s:Button x="144" y="55" label="buscar" width="72" id="browseUpload" click="buscar()"/>
<s:Button x="91" y="113" label="Descargar" click="startUpload_clickHandler(event)" id="startUpload"/>
<!-- panel de descargas-->
</s:Panel>
</s:Group>
aquí les dejo también mi archivo de php por si quieren realizar la prueba:
Código PHP :
<?php
//require_once 'downL.php';
// File: download.php
//$file="prueba_excel.xls";
if (!isset($_GET['file']) || empty($_GET['file'])) {
exit();
}
//$_FILES['Filedata']['prueba_excel.xls']
//file ='C:\xampp\htdocs\flex\clasesp1\nuevoflex\base_datos\Download-debug\images\0.jpg';
$root = "C:/xampp/htdocs/flex/clasesp1/nuevoflex/base_datos/menu desplegable-debug/images/";
$file = basename($_GET['file']);
$path = $root.$file;
$type = '';
if (is_file($path)) {
$size = filesize($path);
if (function_exists('mime_content_type')) {
$type = mime_content_type($path);
} else if (function_exists('finfo_file')) {
$info = finfo_open(FILEINFO_MIME);
$type = finfo_file($info, $path);
finfo_close($info);
}
if ($type == '') {
$type = "application/force-download";
}
// Set Headers
header("Content-Type: $type");
header("Content-Disposition: attachment; filename=$file");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $size);
// Download File
readfile($path);
} else {
die("Archivo no existe !!");
}
?>
