Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
New Developer
Posts: 8
Registered: ‎12-26-2010
My Device: Not Specified
Accepted Solution

Loading a File into ByteArray

[ Edited ]

Hello everyone,

 

First off, I want to thank everybody on this forum. You may not know it, but you have helped me get far with my application.

 

Forgive my stupidity, as I am a noob with ActionScript. But what I'm trying to do is read an excel file which I have stored in my src folder. Its called "t1.xls".

 

I am using a library called as3xls and this is what my code looks like:

 

 

var xls:ExcelFile = new ExcelFile();
var ba:ByteArray = new ByteArray();
			
var f:File = File.applicationDirectory;
f = f.resolvePath("t1.xls");

ba = tempFiles.data;
xls.loadFromByteArray(ba);

 

 

The ExcelFile class is a class that comes with the as3xls library.

 

I'm getting an error when doing this, which is basically telling me that my ByteArray is null. I don't know much, but I suspect that I'm not reading the file correctly. Like I said earlier, the file, "t1.xls" is inside my src folder.

 

I would like to know if I am reading the file properly, and if I am doing something wrong, how can I make a ByteArray from a file in my src folder.

 

Thanks in advance Smiley Happy

Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Loading a File into ByteArray

hey magicman,

 

glad to hear you've found help through the forums! Smiley Happy

 

as for your code, im a little confused as to what tempFile.data represents in this line:

 

ba = tempFiles.data;

 

also try re-writing this

 

var f:File = File.applicationDirectory;
f = f.resolvePath("tl.xls");

 

to

 

var f:File = File.applicationDirectory.resolvePath("tl.xls");

 

good luck!

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 409
Registered: ‎12-10-2010
My Device: PlayBook
My Carrier: N/A

Re: Loading a File into ByteArray

[ Edited ]

i'm assuming here, since i don't see all of your code, but it appears that:

 

ba = tempFiles.data;
xls.loadFromByteArray(ba);

might suppose to be:

 

ba = f.data;
xls.loadFromByteArray(ba);

if that's the case you could reduce your code by writing:

 

var ba:ByteArray = new ByteArray();
ba = File.applicationDirectory.resolvePath("t1.xls").data;

var xls:ExcelFile = new ExcelFile();
xls.loadFromByteArray(ba);

PlayBook Applications:
Drop Swatch
Developer
Posts: 29
Registered: ‎11-14-2010
My Device: Playbook

Re: Loading a File into ByteArray

Hello,

 

You need to load the file first before you can access the ByteArray data of that file.

 

Here is a simple example:

 

 

private function onClick(e:Event):void {
	loadStatus = "Loading file";
	
	_xlsFile = File.applicationDirectory.resolvePath('sample.xls');
	_xlsFile.addEventListener(Event.COMPLETE, onFileLoaded);
	_xlsFile.load();
}

protected function onFileLoaded(event:Event):void
{
	var ef:ExcelFile = new ExcelFile();
	ef.loadFromByteArray(_xlsFile.data);
	loadStatus = "Loaded sheets: " + ef.sheets.length;
}

 

You must also be aware that as3xls is using Flex framework classes so you wont be able to use that library in pure AS3/QNX project.

 

 

Here is the sample Flex Hero project where you can see the whole example.

You will need Flash Builder Burrito to open the project.

 

New Developer
Posts: 8
Registered: ‎12-26-2010
My Device: Not Specified

Re: Loading a File into ByteArray

[ Edited ]

Once again, this forum has proven itself Smiley Happy

 

You guys are AWESOME!

 

JRab and TheDarkln, yeah, it was supposed to f, not tempfiles, I was changing variables because I did not want to type so much, but i guess i forgot to change that.

 

My problem was that I was doing stuff before it was fully loaded. Adding the EventListener fixed this. Thank you guys once again

 

This is my final code:

 

 

private var f:File;
private var xls:ExcelFile;
private var ba:ByteArray;
		
private function loadFile():void{		
	f = new File();			
	ba = new ByteArray();
	f = File.applicationDirectory.resolvePath('t5.xls');
	f.addEventListener(Event.COMPLETE, onFileLoaded);
	f.load();	
}
		
		
protected function onFileLoaded(event:Event):void
{
	xls = new ExcelFile();
	xls.loadFromByteArray(f.data);
			
	var sheet:Sheet = xls.sheets[0];
	(A BUNCH OF CODE)		
}

 

THANKS ONCE AGAIN TO EVERYONE!

 

PS: JRab, you are one of those people who have helped me without knowing it. With the CustomCellRenderer with word wrapping. Thanks!

 

 

Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Loading a File into ByteArray

@magicman0: its great to be appreciated but even greater to know i've have helped! should you need anything else dont be afraid to ask! good luck with the rest of your playbook development! Smiley Happy

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
New Contributor
Posts: 3
Registered: ‎06-17-2011
My Device: non-BlackBerry user
My Carrier: non-BlackBerry user

Re: Loading a File into ByteArray

Hey quick follow up, I am trying to do the following:

 

read cell A1 from Book1.xlsx using actionscript. Can you (or anyone) paste their version of code for setting Excel file into a ByteArray and then read the cell by making use of the as3xls-1.0.swc library. I am a newbie to actionscript but I understand basic concepts of brower,parsers, DOM. And if you can please paste ALL lines (please dont exclude like a library import statement for ex). Thanks in advance!

New Contributor
Posts: 3
Registered: ‎06-17-2011
My Device: non-BlackBerry user
My Carrier: non-BlackBerry user

Re: Loading a File into ByteArray

And I dont understand the significance of "loading" the file. Aren't you just opening a file and reading from it...I thought loading had more to do with rendering...

Highlighted
New Contributor
Posts: 3
Registered: ‎06-17-2011
My Device: non-BlackBerry user
My Carrier: non-BlackBerry user

Re: Loading a File into ByteArray

thanks again in advance!