using writeFile to write binary data

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

User avatar
mistermind
Posts: 131
Joined: 15 Sep 2007, 01:33
Contact:

using writeFile to write binary data

Postby mistermind » 06 Oct 2010, 18:21

Hi there,
I got a tricky one here:
I'm trying to create a jpg file in the server by using the _server.writeFile.
I'm converting the ByteArray to Base64 on the client, sending it via JSON to the server, then converting it back to binary by decoding it.

Code: Select all

_server.writeFile("c:\\test64.jpg", decode64(params.im));

The Base64 string is working just fine and I tested several times, but the problem is when I write the decoded string into a file. The file always get corrupted.
Here is a website you can test it out:

http://decodebase64.com/ (use the example provided in the first comment)

If you try to decode that exact string into bytes and write it to a jpg file, it comes out corrupted.
SELECT * FROM users WHERE clue > 0
0 rows returned.
User avatar
Lapo
Site Admin
Posts: 21288
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 08 Oct 2010, 14:00

In order to write binary files you should use the Java classes.
One quick way to do it would be to download the Apache Commons IO library which offers very high level methods to read/write files. It is very powerful and allows file access in one line of code.

Steps to follow:
-- download the library ( http://commons.apache.org/io/ )
-- add the jar to the lib/ folder in SFS and add the reference to the classpath
-- import the FileUtils class in your code and enjoy :)
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
mistermind
Posts: 131
Joined: 15 Sep 2007, 01:33
Contact:

Postby mistermind » 08 Oct 2010, 15:57

ow yeah :D
thats why I made this tutorial:
viewtopic.php?t=8656

It pretty much does what you mentioned. I even posted a java class ready to be used.

YOU enjoy! :lol:
SELECT * FROM users WHERE clue > 0

0 rows returned.

Return to “Server Side Extension Development”

Who is online

Users browsing this forum: No registered users and 4 guests