0

I have a simple file browser that allows a user to select a local image, I want to manipulate this image before uploading to the server. The problem I'm having is that when I call fileReference.upload() it seems to upload the original file on the user's hard drive rather than the modified bytearray. Am I doing something wrong, is this expected behaviour or a bug?

As a very basic test if I do something like this I still get the original file:

// load the file in to memory
_fileReference.load();

// ... on file reference loaded
trace(_fileReference.data.length); // 230189
_fileReference.data.clear();
trace(_fileReference.data.length); // 0

// With this next line commented out I would expect a 0 bytes file, or an error or something, but instead it happily uploads the original file.
//_fileReference.data.writeBytes(myNewByteArray);

_fileReference.upload(myURLRequest);

According to this post Manipulate file data (byte array) this should work...

Any thoughts greatly appreciated!

Cheers, Simon

Community
  • 1
  • 1

1 Answers1

0

Read the documentation of FileReference carefully. It says that data property is read-only. That means you cannot directly change an image before uploading it.

To change an image, you can use Loader::loadBytes() to get an instance of Bitmap, then change it the way you want, encode it and then upload to your server.

Timothy Kovalev
  • 315
  • 1
  • 7
  • Thanks, good point, it does say read-only. My problem is if I do what you suggest - I'm guessing when you say "then upload to your server" you mean using URLLoader rather than FileReference - I end up needing to use the data property of the URLRequest twice, once for the image and again for the post variables that S3 requires to authenticate the request. But I guess that's a different question. – Simon Adcock Mar 18 '15 at 10:10