Copy link to clipboard
Copied
Hello all, I have a problem with the location where I put the: fileReference.download(urlRequest, "file.zip"); line. If I test this after the event Click on the button works fine! But if I put this inside a function doesnt work, the code:
package {
import flash.display.Sprite;
import flash.display.Graphics;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.net.URLVariables;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequestMethod;
import flash.external.ExternalInterface;
public class Main extends Sprite{
var proxy:String = "proxy.php?data=";
var filename:String = "archivo.zip";
var req:URLRequest = new URLRequest(proxy + filename);
var file:FileReference = new FileReference();
var formattedCode:String;
var formattedEmail:String;
var emailInput:TextField = new TextField();
var codeInput:TextField = new TextField();
var inputHeight:Number = new Number(29);
var inputWidth:Number = new Number(387);
var email_regex:RegExp = /^[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+\.)+[A-Z]{2,4}$/i;
var formatInput:TextFormat = new TextFormat();
var download_btn:Sprite = new Sprite();
public function Main() {
download_btn.graphics.beginFill(0xFFCC00);
download_btn.graphics.drawRect(300, 300, 200, 200);
download_btn.graphics.endFill();
formatInput.size = 20;
formatInput.align = TextFormatAlign.LEFT;
emailInput.setTextFormat(formatInput);
codeInput.setTextFormat(formatInput);
emailInput.type = "input";
codeInput.type = "input";
emailInput.multiline = false;
codeInput.multiline = false;
emailInput.width = inputWidth;
codeInput.width = inputWidth;
emailInput.height = inputHeight;
codeInput.height = inputHeight;
emailInput.alwaysShowSelection = true;
codeInput.alwaysShowSelection = true;
emailInput.x = 75;
codeInput.x = 75;
emailInput.y = inputHeight + (29*3);
codeInput.y = inputHeight + (29*4);
emailInput.border = true;
codeInput.border = true;
addChild(emailInput);
addChild(codeInput);
addChild(download_btn);
download_btn.addEventListener(MouseEvent.CLICK, sendData);
}
private function sendData(e:MouseEvent):void {
var codeVar:String = codeInput.text;
var emailVar:String = emailInput.text;
formattedCode = codeVar.toUpperCase();
formattedEmail = emailVar.toLowerCase();
if(formattedEmail.match(email_regex) && formattedEmail !== "") {
checkCodeFree();
} else {
ExternalInterface.call("console.log", "Bad email address");
}
}
private function checkCodeFree():void {
ExternalInterface.call("console.log", "checkCodeFree");
var url = "checktrue.php";
var loader:URLLoader = new URLLoader;
var urlreq:URLRequest = new URLRequest(url);
var urlvars:URLVariables = new URLVariables;
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
urlreq.method = URLRequestMethod.POST;
urlvars.codeVar = formattedCode;
urlreq.data = urlvars;
loader.addEventListener(Event.COMPLETE, completed);
ExternalInterface.call("console.log", "Calling loader...");
loader.load(urlreq);
}
private function completed(event:Event):void {
ExternalInterface.call("console.log", "completed");
var loader2: URLLoader = URLLoader(event.target);
var code:String = loader2.data.code;
var checked:String = loader2.data.checked;
var edition:String = loader2.data.edition;
var codetrue:String = loader2.data.codetrue;
if(checked == "1" || codetrue == null || codetrue == "") {
if(checked == "1"){
ExternalInterface.call("console.log", "Code in use: " + checked);
}
if(codetrue == null){
ExternalInterface.call("console.log", "Code not exist (null): " + codetrue);
}
if(codetrue == ""){
ExternalInterface.call("console.log", "Code not exist: " + codetrue);
}
}
else{
ExternalInterface.call("console.log", "true_code");
promptDownload();
}
}
public function promptDownload():void {
ExternalInterface.call("console.log", "promptDownload");
file.addEventListener(Event.COMPLETE, completeHandler);
file.addEventListener(Event.CANCEL, cancelHandler);
file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
file.download(req, "file.zip"); // THIS DOWNLOAD DOESNT WORKS
}
private function cancelHandler(event:Event):void {
ExternalInterface.call("console.log", "cancelHandler");
}
private function progressHandler(event:ProgressEvent):void {
ExternalInterface.call("console.log", "progressHandler: name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
}
private function completeHandler(event:Event):void {
ExternalInterface.call("console.log", "completeHandler");
}
private function ioErrorHandler(event:IOErrorEvent):void {
ExternalInterface.call("console.log", "ioErrorHandler");
}
}
}
Copy link to clipboard
Copied
Is possible download() function of "fileReference" works only in a button event?
Copy link to clipboard
Copied
for a web-based app it only works in response to a user even like a mouse click or keyboard event.
When you use this method , consider the Flash Player security model:
allowNetworking
parameter of the the object
and embed
tags in the HTML page that contains the SWF content.Find more inspiration, events, and resources on the new Adobe Community
Explore Now