I have a Desktop Adobe Air app.
I want to add openFileDialog to my app to ask user for the path of his project.
Unfortunatelly, I can't use FileReference - it returns only file name, not path.
private function selectHandler(event:Event):void {
var file:FileReference = FileReference(event.target);
trace("selectHandler: name=" + file.name); //only name!
}
Is there any way to apply openFileDialog in such app and get the path of file, which was selected by user?
flash.filesystem.File.browseForDirectory()
Related
in Actionscript 3 i need to compute data (a string, utf-8), using a secret key (a string, utf-8).
This is the data (string) "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000×tamp=1499827319559"
This is the secret key (string) "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j"
This is the result it must come out (string) "c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71"
Can someone solve this? (the result is correct, but i don't know the code to arrive at the result).
This is what i have tried so far:
// THE DATA
var dataToEncode:String = "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000×tamp=1499827319559";
var byteArrayOfDataToEncode:ByteArray = new ByteArray();
byteArrayOfDataToEncode.writeUTF(dataToEncode); // we write the string into the ByteArray
// THE SECRET KEY
var secretKey:String = "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j";
var byteArrayOfSecretKey:ByteArray = new ByteArray();
byteArrayOfSecretKey.writeUTF(secretKey); // we write the secret key into the ByteArray
// WE COMPUTE THE SIGNATURE
var HMAC_SHA256:HMAC = new HMAC(new SHA256());
var byteArrayOfResultSignature:ByteArray = HMAC_SHA256.compute(byteArrayOfSecretKey, byteArrayOfDataToEncode);
var resultSignature:String = byteArrayOfResultSignature.readUTF();
// WE SHOW THE RESULT SIGNATURE
trace("The result signature is: "+resultSignature);
This HMAC class is created using this library (AS3Crypto):
https://github.com/Atmosphere/ActionScript/blob/master/src/com/hurlant/crypto/hash/HMAC.as
I found, thanks to the commentors, the asnwer to this.
The solution is com.adobe.crypto .
There is a class in the As3corelib (that you can download here
https://code.google.com/archive/p/as3corelib/downloads). You have to import those folders by going to "File-> Actionscript settings", and the click on the source path and then add the "com" folder there.
You will also need to download the Adobe Flex SDK (because some classes in As3corelib use some utility classes in the Flex SDK), unzip it, and then include the framework.swc file (search in the folders of adobe flex, it is in the frameworks/libs folder), going to "File-> Actionscript settings", and the click on the library path and then add the framework.swf there.
Now you are ready to do it, like this:
import com.adobe.crypto.*; // import this, to have access to the classes we need.
// then this code where you need it
var message:String = "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000×tamp=1499827319559";
var secretKey:String = "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j";
var signature:String = HMAC.hash(secretKey, message, SHA256) ;
trace("The signature is: "+signature);
Hope it helps, have a beautiful day!
I use Adobe Flash Professional CS6 + as3 and I have a button to download or save as the video in the desktop but does not execute it? Why ? Where is the mistake?
import fl.video.FLVPlayback;
import flash.display.StageAlign;
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.StageScaleMode;
import flash.media.SoundMixer;
myVideoPlayer.source = "";
addChild (myVideoPlayer);
function loadMyVideo (url:String):void
{
myVideoPlayer.source = url;
myVideoPlayer.visible = true;
}
videosaveas.addEventListener (MouseEvent.CLICK, videosaveas2);
function videosaveas2 (e:Event):void
{
file = new FileReference();
var fileReference:FileReference=new FileReference();
file.save (ByteData, "video.mp4");
}
I don't know if you know the difference between a Flash Player app and an AIR one but I'll put an example for an offline Flash Player app to download a file which is in the same directory as the SWF.
To load the file, I'll use an URLLoader object and then save it using a FileReference one.
Here, don't forget that FileReference.save() is allowed only after a user action (like a mouse click or keypress).
For the example, I'll use the same button for both operations (the user should press the button twice to get the saving file dialog).
var url_loader:URLLoader;
// at the first, we use the button to load the file
btn_download.addEventListener(MouseEvent.CLICK, load_the_file);
function load_the_file(e:MouseEvent): void {
url_loader = new URLLoader();
url_loader.dataFormat = URLLoaderDataFormat.BINARY;
url_loader.addEventListener(Event.COMPLETE, function(e:Event){
btn_download.removeEventListener(MouseEvent.CLICK, load_the_file);
// then we use the same button to download the file
btn_download.addEventListener(MouseEvent.CLICK, download_the_file);
})
url_loader.load(new URLRequest('file_path.ext'));
}
function download_the_file(e:MouseEvent): void {
var file_reference:FileReference = new FileReference()
file_reference.save(url_loader.data, 'default_file_name.ext');
btn_download.removeEventListener(MouseEvent.CLICK, download_the_file);
}
Here, using one button is just an example so you can, for example, show a loading message, use another way to load the file, use two buttons, ...
Then for an online app (a web app), you can use, for example, FileReference.download() :
var file_ref:FileReference = new FileReference();
file_ref.download(new URLRequest('http://www.example.com/file_path.ext'));
For AIR, you have a lot of possibilities using the File, FileStream and the FileReference classes, and you have a lot of example on the net ...
Hope that can help.
I currently have a function that opens up a file browser and limits the selection to the .sol format. I want this function available for easier backwards compatibility support instead of moving the .sol file to the programs appdata folder. When the user selects a .sol, this function is called:
public function onSelect_sol(event: Event): void {
var file: File = File(event.target);
var fileStream: FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
savedData = fileStream.readObject();
trace(savedData);
fileStream.close();
}
However the information I get back from the trace is "null". What do I have to do to make it so I can use the information is the sol? I know this can be done, as the minerva sol editor uses adobe air and can open .sol files: http://blog.coursevector.com/minerva
I am trying to save my graphics using the PNG encoder, and I get two errors.
Scene 1, Layer 'canvas', Frame 2, Line 42, Column 12 1172: Definition PNGEncoder could not be found.
Scene 1, Layer 'canvas', Frame 2, Line 965, Column 24 1120: Access of undefined property PNGEncoder.
Here is all the code revolving saving:
import PNGEncoder; [Recieves the first error]
function export():void
{
var bmd:BitmapData = new BitmapData(board.width, board.height);
bmd.draw(board);
var ba:ByteArray = PNGEncoder.encode(bmd); [Recieves the second error]
var file:FileReference = new FileReference();
file.save(ba, "MyDrawing.png");
}
saveButton.addEventListener(MouseEvent.CLICK,save)
function save(e:MouseEvent):void
{
export();
}
I got this code from a tutorial, so I can't be too sure it is formated right. The tut said that PNGEncoder was an import but according to flash cs3 it is not. Can someone help me straighten this out? I labeled the parts giving me an error, thanks.
You need to download the PNG Encoder and setup Flash to know where to find the new library (the process below can be used for any other externally downloaded SWC's you need to use in future)
1) Create a new folder called Flash_Addons
(the path would be C:\Flash_Addons\ it's just to get you started for now, later change the setup to a prefered folder name/location)
2) Download the AS3 CoreLib (the "Download ZIP" option at bottom/right-hand side
3) Inside the Zip is a folder (I think its called "as3corelib-.93") copy that to your Flash_Addons folder so it's now a sub-folder.
4) Now to tell Flash where to find it.. In your CS got to Edit-->Preferences (CTRL+U shortcut) and click on "ActionScript" in Category list then when shown click ActionScript 3.0 Settings
Now you will see three boxes (Source path/ Library path/ External path).
(for these below, click "+" to add new entry, then either type path or click the folder icon to browse)
Source path: You want it to add.. C:\Flash_Addons\
Library path: You want it to add.. C:\Flash_Addons\as3corelib-.93\lib
(note: the "lib" folder contains the actual as3corelib.SWC file, that's why we put direct path to it. When you want to add any new SWC library just add path to the list of entries (example):
C:\Flash_Addons\as3corelib-.93\lib
C:\Flash_Addons\Useful_Library_Folder_of_SWC_file\
etc... etc..
Now you're ready to export PNG..
import com.adobe.images.PNGEncoder; //fixes the errors
saveButton.addEventListener(MouseEvent.CLICK, save)
function save(e:MouseEvent):void
{ export(); }
function export():void
{
var bmd:BitmapData = new BitmapData(board.width, board.height);
bmd.draw(board);
var ba:ByteArray = PNGEncoder.encode(bmd);
var file:FileReference = new FileReference();
file.save(ba, "MyDrawing.png");
}
I need to know how to load unknown number of images from external folder without using XML
help me please,
thank you
So from your comments I'm assuming this is an AIR Application, so you can access the filesystem via File class.
First of all, you need to get a File object that points to your folder, the easiest way is to hardcode it. A slightly more complex approach would involve opening a dialog, where the user can select his desired folder (using File.browseForOpen).
Let's take the easy route and define a constant path to the folder, here it's a folder called "images" in the users documents folder:
File imageFolder = File.documentsDirectory.resolvePath("images");
Once we have a folder instance, we can use the getDirectoryListing method to list all files within that folder. Here's an example:
// create a vector that will contain all our images
var images:Vector.<File> = new Vector.<File>();
// first, check if that folder really exists
if(imageFolder.exists && imageFolder.isDirectory){
var files:Array = imageFolder.getDirectoryListing();
for each(var file:File in files){
// match the filename against a pattern, here only files
// that end in jpg, jpeg, png or gif will be accepted
if(file.name.match(/\.(jpe?g|png|gif)$/i)){
images.push(file);
}
}
}
// at this point, the images vector will contain all image files
// that were found in the folder, or nothing if no images were found
// or the folder didn't exist.
To load the files into your application, you can do something like this:
for each(var file:File in images){
// use a FileStream to read the file data into a ByteArray
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
var bytes:ByteArray = new ByteArray();
stream.readBytes(bytes);
stream.close();
// create a loader and load the image into it
var loader:Loader = new Loader();
// use the loadBytes method to read the data
loader.loadBytes(bytes);
// you can add the loader to the scene, so that it will be visible.
// These loaders will all be at 0, 0 coordinates, so maybe change
// the x and y coordinates to something more meaningful/useful.
this.addChild(loader);
}
Can you elaborate a little bit better the question? If it's a user action (i.e. the user needs to upload some photos) I'd use the File API - you can see examples here - otherwise, if it's from the server side I'd use a PHP or Phyton script.
Assuming that your application is Air (Desktop application), this code will be useful:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
public function init():void{
var fs:FileStream = new FileStream();
var f:File = new File("c:/imgTest.jpg");
var b:ByteArray = new ByteArray();
fs.open(f, FileMode.READ);
fs.readBytes(b,0,fs.bytesAvailable);
idImg.source = b;
fs.close();
}
]]>
</mx:Script>
<mx:Image id="idImg" width="100%" height="100%"/>
</mx:WindowedApplication>
Place an image in c:/imgTest.jpg. Note that this image is outside from your project path.
You have other options to load images, but these must be accessible by URL, or should be in the path of the project.
Here a links that will be usefull for load images in Flex Air and Web:
Working with File objects in AIR
Image as ByteArray in Flex
Image Control
Programatically load images in Flex.
Note: i tried only with JPG files, i do not know if this works with other types.