When xml is changed the swf file doesn't change - asp.net

I am trying to read values from XML to swf file ,so i have a xml file like this :
<?xml version="1.0" encoding="utf-8"?>
<GALLERY>
<IMAGE TITLE="ssasa">image1.jpg</IMAGE>
<IMAGE TITLE="oooo">image2.jpg</IMAGE>
<IMAGE TITLE="shop">image3.jpg</IMAGE>
</GALLERY>
In my AS3 flash file i have this code :
import flash.system.SecurityDomain;
import flash.system.Security;
var xml:XML;
var xmlList:XMLList;
var xmlLoader:URLLoader = new URLLoader();
Security.loadPolicyFile("http://localhost:15979/crossdomain.xml");
xmlLoader.load(new URLRequest("http://localhost:15979/default.aspx"));
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
function xmlLoaded(event:Event):void
{
xml = XML(event.target.data);
xmlList = xml.children();
trace(xml.IMAGE[1].#TITLE);
txtname.text=xml.IMAGE[1].#TITLE;
}
In my timeline i have this :
As you can see i call my acionscript code in frame 22,but my problem is when i changed the data in xml file the swf file ,it doesn't apply to swf file and the swf file shows the old text on the screen ,but after closing the file it works fine and the new text appears.
I think it is because the iis cachs the data .!!!
best regards

I finally add this value to my request :
var request:URLRequest = new URLRequest("http://localhost:15979/default.aspx");
var variables:URLVariables = new URLVariables();
variables.nocache = new Date().getTime();
// set up other URLVariables here
request.data = variables;

Related

Create pdf using flex

I am trying to create PDF file using flex.
There are some problem which i am facing..
when i have less text to display it's working fine. but when there are lots of text which have to display it's overlap.
is there any PDF component which is expand according to the display text.
Thanks in advance
Enjoy creating .pdf file using this code
import org.alivepdf.pdf.PDF;
import org.alivepdf.saving.Method;
import org.alivepdf.fonts.*;
import org.alivepdf.pages.Page;
import org.alivepdf.display.Display;
import org.alivepdf.layout.*;
private var pdf:PDF;
private var file:File;
[Embed( source="bg.jpg", mimeType="application/octet-stream" )]
private var pngBytes:Class;
public function generate ():void
{
var pdf:PDF = new PDF( Orientation.PORTRAIT, Unit.MM, Size.A4 );
pdf.setDisplayMode( Display.FULL_PAGE, Layout.SINGLE_PAGE );
var newPage:Page = new Page ( Orientation.PORTRAIT, Unit.MM, Size.A4 );
pdf.addPage( newPage );
pdf.setFont(FontFamily.ARIAL , Style.NORMAL, 12);
pdf.addText("This is a sample text",5,15);
pdf.drawCircle(25,35,15);
pdf.addPage();
pdf.addImageStream( new pngBytes() as ByteArray );
var fs:FileStream = new FileStream();
file = File.desktopDirectory.resolvePath("testPage.pdf");
fs.open( file, FileMode.WRITE);
var bytes:ByteArray = pdf.save(Method.LOCAL);
fs.writeBytes(bytes);
fs.close();
}
We can use "AlivePDF" to generate PDF from Flex.
AlivePDF is a client side AS3 PDF generation library for Adobe Flash, Flex and AIR. It is a Open source library.
Library File and Documentation
http://code.google.com/p/alivepdf/
http://code.google.com/p/alivepdf/downloads/list
Reference
http://alivepdf.bytearray.org/

Converting a flex component w/images to bitmap

I am trying to take images I am placing in a flex canvas component to a bitmap. I was able to get to the point where I'm not getting an error but then no image shows up and the image I save out as a jpg is blank. I imagine I'm not setting the bitmap data correctly but can't figure out what I am doing wrong.
Here is the code where I am converting it to a bitmap:
var imageSnap:ImageSnapshot = ImageSnapshot.captureImage(_renderPop);
var imageByteArray:ByteArray = imageSnap.data as ByteArray;
var bLoader:Loader = new Loader();
bLoader.loadBytes(imageByteArray);
var bmd:BitmapData = new BitmapData(500,500);
bmd.draw(bLoader);
var imgTest:Image = new Image();
imgTest.source = bmd;
_renderPop.renderCanvas.addChild(imgTest);
var fileRef:FileReference = new FileReference();
fileRef.save(bLoader, 'testImage.jpg');
_renderPop.renderCanvas is where I am placing the images. Anybody see anything wrong?
Your "loader" code is wrong. Just after capturing image you may at once save data with FileReference:
var imageSnap:ImageSnapshot = ImageSnapshot.captureImage(_renderPop);
var fileRef:FileReference = new FileReference();
fileRef.save(imageSnap.data, 'testImage.png');
That imageSnap contains not BitmapData but png image bytes. In order to show image you need to capture BitmapData but not image and create Bitmap from bitmap data:
var bmd:BitmapData = ImageSnapshot.captureBitmapData(_renderPop);
var imgTest:Image = new Image();
imgTest.source = new Bitmap(bmd);
_renderPop.renderCanvas.addChild(imgTest);
In result locally testImage.png is created on file system and it is shown in canvas. If you need jpg you should specify:
var imageSnap:ImageSnapshot = ImageSnapshot.captureImage(_renderPop, 0, new JPEGEncoder());
In your code:
var bLoader:Loader = new Loader();
bLoader.loadBytes(imageByteArray);
...you're assuming that the bytes are being loaded immediately; try putting an event listener on the loader as follows:
bLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
private function completeHandler(event:Event):void
{
// ... the rest of your code goes here
}
The loadBytes functions works like the load function in that they are both asynchronous processes so you'll need the event listener. It is a little counter-intuitive, and i've made the same mistake myself several times.
If that doesn't work, maybe leave out the contentLoaderInfo property, but the above should work...
Let me know if you come right :)

How to embed SWF in Flex and get the timeline code in the embedded SWF?

my client want to have all resources embedded into the Main SWF which i create with Flex. The UI itself origins form a .FLA that must have timeline actionscript (AS3). After I embed the SWF there seems to be NO timeline AS in the embedded SWF. Is this possible to solve?
I Embed like this:
public var templ: TemplateBase;
[Embed(source="images/template_banner.swf", mimeType="application/octet-stream")]
public var TemplateSWF:Class;
...
var ba : ByteArray = new TemplateSWF() as ByteArray;
var l : Loader = new Loader();
l.loadBytes(ba);
l.addEventListener(Event.ADDED_TO_STAGE, onTemplateAdd);
addChild(l);
private function onTemplateAdd(evt:Event):void{
templ = evt.target.contentLoaderInfo.content;
}
In template_banner.swf there is a stop(); in frame 1, and some code in frame 2.
I trace in both frames but nothing is showing in the Flex (4) debugger.
After the swf is added to stage I do play() in the TemplateBase class.
The thing is that play and all the AS code in the loaded SWF is totally dead.
My question is: Is there a way to keep the timeline AS code in the embedde SWF? And yes I need to embed the SWF into the Flex main file :/
Thanks,
Rob
From what I'm seeing in the code, you are doing loadBytes which is asynchronous... and then waiting to the ADDED_TO_STAGE, and in the event listener you are accessing the content...
Because the loadBytes is asynchronous, you should wait for the loading to complete.
In the complete listener you should access the loaded content.
I would totally avoid the ADDED_TO_STAGE.
do it like this:
public var templ: TemplateBase;
[Embed(source="images/template_banner.swf", mimeType="application/octet-stream")]
public var TemplateSWF:Class;
var ba : ByteArray = new TemplateSWF() as ByteArray;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
loader.loadBytes(ba);
private function completeHandler(event:Event):void {
templ = evt.target.contentLoaderInfo.content;
}

FileReference and HttpService Browse Image Modify it then Upload it

I am trying to do an image uploader, user can:
- browse local file with button.browse
- select one and save it as a FileReference.
- then we do FileReference.load() then bind the data to our image control.
- after we make a rotation on it and change the data of image.
- and to finish we upload it to a server.
To change the data of image i get the matrix of the displayed image and transform it then i re-use the new matrix and bind it to my old image:
private function TurnImage():void
{
//Turn it
var m:Matrix = _img.transform.matrix;
rotateImage(m);
_img.transform.matrix = m;
}
Now the mater is that i really don't know how to send the data as a file to my server cause its not stored in the FileReference and data inside FileReference is readOnly so we can't change it or create a new, so i can't use .upload();.
Then i tried HttpService.send but i can't figure out how you send a file and not a mxml.
You can use URLLoader to send Binary ByteArray to server, like:
var urlRequest : URLRequest = new URLRequest();
urlRequest.url = 'path to your server';
urlRequest.contentType = 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary();
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = UploadPostHelper.getPostData( 'image.jpg', byteArray );
urlRequest.requestHeaders.push( new URLRequestHeader( 'Cache-Control', 'no-cache' ) );
// create the image loader & send the image to the server:<br />
var urlLoader : URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load( urlRequest );
First get bitmapdata for the image:
// set up a new bitmapdata object that matches the dimensions of the captureContainer;
var bmd : BitmapData = new BitmapData( captureContainer.width, captureContainer.height, true, 0xFFFFFFFF );
// draw the bitmapData from the captureContainer to the bitmapData object:<br />
bmd.draw( captureContainer, new Matrix(), null, null, null, true );
Then get byteArray:
var byteArray : ByteArray = new JPGEncoder( 90 ).encode( bmd );
and use the above URLLoader code to send image to server.
It will work fine, except you wouldn't get the file upload progress like the one you get from FileReference.upload. If you can make upload progress work using URLLoader, please post your answer here.

How can you save out a String into a file in AS3?

I have a string the user has typed and I want to save it into a file on the users harddrive. Can you do that? And if so, how?
Yes you can, with FileReference.
This is basically how it's done:
var bytes:ByteArray = new ByteArray();
var fileRef:FileReference=new FileReference();
fileRef.save("fileContent", "fileName");
Doesn't look too hard, does it?
And here's a video-tutorial on it too:
http://www.gotoandlearn.com/play?id=76
And the documentation:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/
Hope that helps.
Since I had a function to output bytes to a file (because I was doing something with bitmaps), I reused it to output a string as well, like this:
var filename:String = "/Users/me/path/to/file.txt";
var byteArray:ByteArray = new ByteArray();
byteArray.writeUTFBytes(someString);
outFile(filename, byteArray);
private static function outFile(fileName:String, data:ByteArray):void {
var outFile:File = File.desktopDirectory; // dest folder is desktop
outFile = outFile.resolvePath(fileName); // name of file to write
var outStream:FileStream = new FileStream();
// open output file stream in WRITE mode
outStream.open(outFile, FileMode.WRITE);
// write out the file
outStream.writeBytes(data, 0, data.length);
// close it
outStream.close();
}
In addition, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3.
You can also have a look the following example:
http://blog.flexexamples.com/2008/08/25/saving-files-locally-using-the-filereference-classs-save-method-in-flash-player-10/
In Flex 3 no you can't do it unless you upload the file to the server and then download the file via a url to the desktop.
In Air or Flex 4 you can save it directly from the application to the desktop as detailed above.

Resources