Dan Naim
Dan Naim

Reputation: 161

Upload file to server without using FileReference.browse()

I am trying to upload file to server with the FileReference class. the file is mp3 that was encoded in runtime without saving him on the local hard drive.

Is it possible to fit in the mp3Encoder into the FileReference somehow?

Thanks

Upvotes: 0

Views: 1717

Answers (1)

Jude Fisher
Jude Fisher

Reputation: 11294

Assuming you have the mp3 as a ByteArray (not sure how else it would exist at runtime if you haven't saved it to disk), you can use the URLLoader class, like this:

var scriptRequest:URLRequest = new URLRequest("PATH_TO_YOUR_UPLOAD_SCRIPT");
var scriptLoader:URLLoader = new URLLoader();
var scriptVars:URLVariables = new URLVariables();

scriptLoader.addEventListener(Event.COMPLETE, handleLoadSuccessful);
scriptLoader.addEventListener(IOErrorEvent.IO_ERROR, handleLoadError);

scriptVars.var1 = MP3_AS_BYTE_ARRAY;

scriptRequest.method = URLRequestMethod.POST;
scriptRequest.data = scriptVars;

scriptLoader.load(scriptRequest);

function handleLoadSuccessful($evt:Event):void
{
    trace("Message sent.");
}

function handleLoadError($evt:IOErrorEvent):void
{
    trace("Message failed.");
}

Then on the server, you just need to save the stream to a file and give it the correct mp3 mime type.

There's a decent tutorial, from which this example is adapted, here: http://evolve.reintroducing.com/2008/01/27/as2-to-as3/as2-%E2%86%92-as3-loadvars-as3-equivalent/

And if you need to send your request as multipart/form-data (so it appears to be an attached file), there's a good example of this here: http://marstonstudio.com/2007/10/19/how-to-take-a-snapshot-of-a-flash-movie-and-automatically-upload-the-jpg-to-a-server-in-three-easy-steps/

Upvotes: 1

Related Questions