Flash AS3 Read Text File - actionscript-3

I simply need to read a text file from my computer, or a website. I've tried everything, and nothing so far has worked. It should be extremely simple, just reading a text file from a website, like http://foo.com/foo.txt/, but I've tried everything, and nothing I have seen on Google comes even close to working. I don't care how I get the problem solved, as long as I can do it.

To read the content of a file, just use a URLLoader:
// Define the path to the text file.
var url:URLRequest = new URLRequest("file.txt");
// Define the URLLoader.
var loader:URLLoader = new URLLoader();
loader.load(url);
// Listen for when the file has finished loading.
loader.addEventListener(Event.COMPLETE, loaderComplete);
function loaderComplete(e:Event):void
{
// The output of the text file is available via the data property
// of URLLoader.
trace(loader.data);
}
For stuff on another domain, you will need to have a crossdomain file hosted to be able to load the text file.

Related

Actionscript-3 file access from SWF embedded into PDF

My SWF accesses a text file with the following code.
var myTextLoader:URLLoader = new URLLoader();
myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
myTextLoader.load(new URLRequest("C:\\Users\\Chris\\Desktop\\moves.txt"));
I have now embedded my SWF inside a PDF and it stops working, is there a way to do this?

How make with AS3 downloading MP3 files in the background mode and plays it?

I need help with AS3 make downloading MP3 files in the background mode. Man goes to my site and it will automatically load MP3 file on his computer and plays it. I read these examples here http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLLoader.html but do not know how to do it? Prompt what code and how to use it properly?
You can directly load a Sound object with an URLRequest instead of using a separate Loader or URLLoader. A code could look like this:
// this code should reside in a function somewhere
var request:URLRequest = new URLRequest("http://your.site.com/your.mp3");
var sound:Sound = new Sound(request;
sound.addEventListener(Event.COMPLETE, completeHandler);
// this function should be at top level anyway
private function completeHandler(event:Event):void
{
var loadedMP3:Sound = Sound(event.target);
if (loadedMP3) loadedMP3.play();
}
Extra error handling is up to you.

How to auto load URL through SWF banner in the background?

I am new here just signed up and I did not find information on how to classify this topic on specific section.
I am using adobe flash professional to create a small banner to auto load one of my own URL for analytics. Is this possible? I found one solution on another user's topic which was (AS 3):
var req:URLRequest("http://your_request");
var loader:URLLoader = new URLLoader ();
loader.addEventListener(Event.COMPLETE, onLoadComplete);
loader.load(req);
private function onLoadComplete(ev:Event):void
{
var result:String = ev.target.data;
}
But this doesn't seem to work. How can I test that the full webpage is loaded 'invisibly' in the background? If "http://your_request" has cookies for example, I can check if the cookies were created, but I do not know if with this method I'll load the cookies to check if this is working at the first place.
There is no way to get http-cookies natively. But you can do this with a js-helper-function and an ExternalInterface.call. See this answer: How do I access cookies within Flash?

AS3 POST png to PHP

Have read many similar but no solutions: I'm trying to do something (that i think is) really simple,
Take a photo from gallery or camera - and POST this to a URL. No saving / returning to application. The PNG is just used by the webpage in display and user moves onward.
public function postPicture():void {
PNGEncoder2.level = CompressionLevel.FAST;
var encoder : PNGEncoder2 = PNGEncoder2.encodeAsync(_bitmapData);
encoder.targetFPS = 12;
encoder.addEventListener(Event.COMPLETE, function (e):void {
var png : ByteArray = encoder.png;
var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var jpgURLRequest:URLRequest = new URLRequest("https://myurl.com/mypage");
jpgURLRequest.requestHeaders.push(header);
jpgURLRequest.method = URLRequestMethod.POST;
jpgURLRequest.data = png;
navigateToURL(jpgURLRequest, "_blank");
});
}
Also to note, we're getting some odd characters on the end of the URL: C9%90NG
Hunch is that I'm decoding and then not encoding properly before sending i.e. these characters some part of raw image info.
When I test from chrome POST extension with another image, server side stuff works fine so guessing a problem with AS3.
basically its working when I use chrome to browse > my.png and POST it at the page. Am I missing something with the AS3 png encoder? i.e. how to turn that bmpdata into a 'file' rather than an array of bytes.
thankyou
You need to use multipart upload. It's a common situation with a well defined standards for that. You can implement it by yourself but it will take some time and efforts.
What I've used before is the Multipart URLLoader Class provided by Eugene Zatepyakin, one the best Flash guru's out there :)
It's some kind of old but does the job extremely well. You can add variables, files, whatever content you want to your loader and simply submit it. There are examples with the php side of the thing so you can understand what's going on.

AS3 , Facebook API - Can't retrieve image from event object after image loads

I am getting a mysterious result when trying to load profile images from a logged in Facebook user's friends list into an as3 canvas app. In pretty much every (non-facebook) application I've made where loading an image is required I use the following:
private function loadPic():void
{
var url:String = Facebook.getImageUrl(_friendslistObjectArray[_loadCount].id);
_picLoader = new Loader();
var req:URLRequest = new URLRequest(url);
_picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onPicLoaded);
_picLoader.load(req);
}
private function onPicLoaded(evt:Event):void
{
_picLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onPicLoaded);
// grab image from loader event to use it
var image = evt.target.content;
trace("this trace won't even show up and the program silently fails unless the line above this is removed");
_loadCount++;
loadPic();
}
I can't retrieve the image from the event object, and I am given no error at all. It all just fails silently. If I remove the line var image = evt.target.content, the process runs through the whole array.
I can't think of any reason this would occur. Furthermore, if I look in firebug's net activity, I see I see the images get loaded as long as I do not have the line I just mentioned included. I just can't seem to get the image data from the event object to add it to stage or a container movieclip.
This should be very easy. I'm racking my brain here trying to figure out what I missed or what is wrong. Any thoughts? Thanks!
Found the answer:
This line:
Security.loadPolicyFile("http://profile.ak.fbcdn.net/crossdomain.xml");
Placed right before _picLoader.load(req); fixes the problem.
It was a crossdomain error, but for some unknown reason the error did not report until I threw var image = evt.target.content; into a try/catch and read the error message.
Props to this post for the answer:
Load profile image from Facebook with Actionscript 3
the evt.target should be a LoaderInfo object, try the code here:
var image = (evt.targe as LoaderInfo).loader.content;