Upload Photo in Flex, set the URLRequest - actionscript-3

In my Flex Application doing Image upload for local system.
Actually i'm using FileReference class in this class have load() it is only in FP-10.
so i'm doing another way using upload() but this method taking URLRequest object.
private const FILE_UPLOAD_URL:String
fileRef.upload(new URLRequest(FILE_UPLOAD_URL));
so problem is how to get file url in local system..
give me example for this help lot..

Try this:
public class Uploader{
private var fileReference:FileReference;
private static const FILE_UPLOAD_URL:String = "http://somewebsite.com/images/..."
public function Uploader()
{
fileReference = new FileReference();
fileReference.addEventListener(Event.SELECT, onFileSelect);
}
//Call this method to start browsing on local filesystem
private function doUpload():void
{
fileReference.browse();
}
//After selecting a file you can now upload it
private function onFileSelect():void
{
fileReference.upload(new URLRequest(FILE_UPLOAD_URL));
}
}
A full, working example can be found here
Also make sure to check out the Livedocs on this matter
Cheers

Related

ActionScript 3 not showing jpg

Im working with Starling and im trying to show a jpg on my background, but it doesnt shows up. Here is the embed code:
[Embed(source="../media/img/backgroundmenu.jpg")]
public static const BgWelcome:Class;
Later on I cast it into a bitmap and make it a texture. Im getting the following error at the getter of the bitmap:
ReferenceError: Error #1069: Property metaData not found on resources.Assets_BgWelcome and there is no default value.
The jpg is at the exact folder, tried to place it with the absolute path, but nothing apears.
Thanks in advance.
Here's a nice way to go about creating a convenient container for all your graphics using starling:
public class Art {
[Embed(source = "../textures/foo.jpg")] private static const FooBitmap:Class;
public static var FooTexture:Texture = MakeTexture(FooBitmap);
private static function MakeTexture(_Texture:Class):Texture {
var bitmap:Bitmap = new _Texture();
return Texture.fromBitmap(bitmap);
}
}
Usage:
var foo:Image = new Image(Art.FooTexture);

AS3 Accessing a class property that relies on URLLoader

I'm facing some problems in AS3. For example, I have two classes Car.as and ReadXML.as as follows:
Car.as
public class Car{
public function get price():String{
var priceXML:ReadXML = new ReadXML('price.xml');
return priceXML.file;
}
}
ReadXML.as
public class ReadXML{
public var file:XML;
public var loader:URLLoader;
public function ReadXML(fileName:String):void{
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, _loadComplete);
loader.load(new URLRequest(fileName));
}
private function _loadComplete(e:Event):void{
file = new XML(loader.data);
loader.removeEventListener(Event.COMPLETE, _loadComplete);
}
}
But when I try to access the price property,
var carObj:car = new Car();
trace(carObj.price)
it returns null which I presume is because the URLLoader hasn't been finished. So what is an alternative or solution to this? Thanks!
I think loading an XML file every time you want to check an instance's 'price' property is the problem.
I'd recommend loading the XML file as part of your application's initialisation, and including the _loadComplete function as part of that initialisation; When the XML is loaded, your application can then continue and instantiate as many 'new Car()'s as it fancies - no delay will be required if the XML is pre-loaded.

AIR dispatch event with parameter

I have a form with a search field.
When user press enter key, I use httpservice to send a query to mySQL database.
In some case (a lot) there are several record, so a new window is opening to show those record with a datagrid to let user chose the good result.
My problem is how to send selected information to the first window (with text field).
I gess that dispatch event is the way but I don't found how to use!
Can you help me to find a solution.
Thanks
If you are trying to communicate within an MDI environment I suggest that you use some kind of shared model ( aka Mediator or Presentation Model ) that keeps a contract between the desired windows.
class SelectionPM{
[Bindable]
public var selectedItem:Object;
}
Use case:
Window1 has an instance of SelectionPM, when you open Window2 you pass
SelectionPM instance to it, then update SelectionPM.selectedItem
property on changing selection in the Window2 datagrid. That will
propagate the binding chain up to Window1, where you can use the
SelectionPM.selectedItem as you wish.
Ideally you would use an IoC container for model injection but that is another story.
That might seem like a lot of work but if you keep this methodology across your app you will benefit from it.
Cheers!
Here's a set of four classes as a basis. Obviously you don't want to be doing the actual work in the constructors as below.
public class App
{
public static var eventDispatcher:EventDispatcher = new EventDispatcher();
public function App()
{
new Window1();
}
}
class AppEvent extends Event
{
public static const DATA_READY:String = "APPEVENT.DATA_READY";
public var data:Object;
public function AppEvent( type:String, data:Object )
{
super( type );
this.data = data;
}
}
class Window1
{
public function Window1()
{
App.eventDispatcher.addEventListener( AppEvent.DATA_READY, onDataReady );
...DO STUFF...
new Window2();
}
private function onDataReady( evt:AppEvent ) : void
{
...DO STUFF WITH "evt.data"....
}
}
class Window2
{
public function Window2()
{
...GET DATA FROM SERVER AND PUT IT IN "data"...
App.eventDispatcher.dispatchEvent( new AppEvent( AppEvent.DATA_READY, data ) );
}
}
</pre>

Is it possible to create dynamic embed function?

Is it possible to create dynamic embed function in ActionScript3
for example like this
public function embedImage(path:String):Bitmap{
[Embed(source = path, mimeType = "image/png")]
var NewBitmapClass:Class;
var image:Bitmap=new NewBitmapClass();
return image;
}// tried it, it doesnt work
or maybe in some other way, or even if it is at all possible?
The closest you can get with the "dynamic" part, is to create a wrapper class, where you define your images, and you can get them as Bitmap later on by an id.
Unfortunately the properties are public, otherwise the hasOwnProperty function doesn't return true. (If someone finds a better way, please let me know)
See below:
package {
import flash.display.Bitmap;
public class DynamicEmbed {
[Embed(source = "../images/cat.jpg")]
public var cat : Class;
[Embed(source = "../images/parrot.jpg")]
public var parrot : Class;
[Embed(source = "../images/pig.jpg")]
public var pig : Class;
[Embed(source = "../images/quail.jpg")]
public var quail : Class;
public function DynamicEmbed() {
}
public function getBitmap(id : String) : Bitmap {
if(hasOwnProperty(id)) {
var bitmap : Bitmap = new this[id]();
return bitmap;
}
return null;
}
}
}
Embedded elements are embedded at compile time. You can't dynamically embed something at compile time... If you want to load resources dynamically, use the Loader.
No, embed source is embedded at compile time. You can not embed anything at run time. That's what embed means, embedding during building the swf.

AS3 driving me nuts

Ok here is what I am currently trying to do. I have a class called vdata.as which takes 2 paramaters both are strings sent from the main stage. Parameter one is the location for an XML file that I need to open and read. The second parameter is the name of the video I am currently looking for.
Now I can get the data from the XML file and display it with out any issue if its called from my class but when I try to access any of it from the stage I get undefined.
import flash.net.*;
import flash.display.*;
import flash.events.*;
public class videoData
{
private var mName:String;
private var mLink:String;
private var mCategory:String;
public static var elementArray:Array;
// Constructor
public function videoData(xmlPath:String,xmlVidSrc:String,pMC:MovieClip)
{
pXmlPath = xmlPath;
pXmlVidSrc = xmlVidSrc;
xmlloader = new URLLoader();
elementArray = new Array();
}
public function getXML()
{
XMLData();
}
private function XMLData()
{
xmlloader.load(new URLRequest(pXmlPath));
xmlloader.addEventListener(Event.COMPLETE,parseXMLData);
}
private function parseXMLData():void
{
var x:XML = new XML(xmlloader.data);
Init(x);
}
private function Init(m:XML):*
{
var i:Number;
for(i=0; i<m.videos.videoname.length(); i++)
{
if(m.videos.videoname[i].#name == pXmlVidSrc)
{
videoData.elementArray.push(m.videos.videoname[i].#name);
videoData.elementArray.push(m.videos.videoname[i].#category);
videoData.elementArray.push(m.videos.videoname[i].link.#url);
}
}
}
}
When I call it from the main stage the code is as follows.
var xData:videoData = new videoData(xmlPath,vidSrc,this);
xData.getXML();
then when I try to access any elements of videoData.elementArray they come up undefined...
Im just smacking my head on my desk trying to figure this out any help would be great.
Why is elementArray a static var, you only need to make it public to use it outside the function.
I'm quite confusing but you may want to try a debugging tool like "De MonsterDebugger", I would start by tracing xmlloader.data in the parseXMLData function.
"addEventListener" doesn't "fire"...the event does. You'll need to add a boolean to state for the stage that elementArray has been populated and set that after the init function.
Is elementArray something that needs to be true across all instances of videoData? If not, it shouldn't be static. You can use MovieClip(this.root).xData to access that instance of the video class from one of your other classes.
If the event has completed and the array is still empty - then it wasn't populated by your parser. You can also do checks to see if the elementArray.length > 0.
EDIT in response to comment:
as a public member or preferably a read-only property make a boolean variable:
var parseComplete:Boolean;
Set it to false in your constructor.
Then, after your call to "Init" in your Event.COMPLETE callback set:
parseComplete=true;
Then make sure parseComplete == true before you ever access elementArray. If you're waiting for the parser to complete you might want to set a timeout or some sort of try/catch mechanism just in case there are any unforeseen errors that would cause some sort of:
while( !xData.parseComplete ) { }
To loop indefinitely. It all depends on the usage. Personally I'd probably add a callback from the event listener to the stage to trigger whatever is supposed to happen next.