PDA

View Full Version : AS3 File Upload



Blommestein
July 26th, 2007, 01:43 PM
I cannot seem to get this to work.

I have everything open-source here: http://ustudios.net/blommestein/test/

The actionscript:


var fileRef:FileReference = new FileReference();
var fileFil:FileFilter = new FileFilter("GIF Image", "*.gif;");
fileRef.addEventListener(Event.SELECT, selectHandler);
fileRef.addEventListener(Event.COMPLETE, completeHandler);
try {
var success:Boolean = fileRef.browse([fileFil]);
} catch (error:Error) {
trace("Unable to browse for files.");
}
function selectHandler(event:Event):void {
var request:URLRequest = new URLRequest("upload.php")
try {
fileRef.upload(request);
navigateToURL(request);
} catch (error:Error) {
trace("Unable to upload file.");
}
}
function completeHandler(event:Event):void {
trace("uploaded");
}

The php script (upload.php):


<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>

When running http://ustudios.net/blommestein/test/Test.swf , it should just create a temporary copy on the server, however NONE of the properties are read, as shown below:


Upload:
Type:
Size: 0 Kb
Stored in:

Can anybody tell me what is wrong, or link me to a working upload script?

Blommestein
July 27th, 2007, 01:40 AM
Just bringing this back up since I'm sure someone knows a solution, else flex wouldn't have the upload function. :smirk:

Thanks.

Blommestein
July 29th, 2007, 08:04 AM
Anyone? :)

Blommestein
July 29th, 2007, 07:16 PM
Found a good example: http://www.lastashero.com/tutorials/2005/10/creating_a_file_upload_applica.php

Problem Solved. :)

joshchernoff
July 29th, 2007, 09:36 PM
::::::not worth my time or effort

Blommestein
July 29th, 2007, 10:23 PM
Not to be rude but that's not your own. (Correct me if Im wrong) You just took the AS off the Adobe Flex 2 Lang Reference, then changed


private function progressHandler(event: ProgressEvent):void {
var file:FileReference = FileReference(event.target);
trace("progressHandler: name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
}to


private function progressHandler(event:ProgressEvent):void {
var file:FileReference = FileReference(event.target);
trace("progressHandler name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
trace(event.bytesLoaded / 1000 + "KB loaded out of " + event.bytesTotal / 1000 + "KB "+ uint(100 * event.bytesLoaded / event.bytesTotal)+"%");

}
and used the same php script from the link I provided above.

Anyway mine will work as posted above, but with the new php script from the link.

joshchernoff
July 29th, 2007, 11:59 PM
:::not worth my time or effort

joshchernoff
July 30th, 2007, 12:18 AM
::::::not worth my time or effort

Blommestein
July 30th, 2007, 12:21 PM
just from reading I decided to make my own.
not to be rude but I never claimed it as my own. ;)


why are you not using the Event handlers in the AS3?

If you looked above...
I am, seeing as it wouldn't work without the select one. You should notice these things instead of underlining it for what, emphasis?

fileRef.addEventListener(Event.SELECT, selectHandler);
fileRef.addEventListener(Event.COMPLETE, completeHandler);

You'd have to have a select handler, I just keep the complete handler for confirmation.

All the others, opens, cancel, httpstatus, are useless to me. So that's why I am using some event handlers.


why are you trying to echo the file info from the php file?
To make sure the file data was being sent to the server. I just hadn't known you use ['filedata'] array instead of ['file'] array. The last as hero pointed that out.

joshchernoff
July 30th, 2007, 01:45 PM
have fun trying to get help around here.:block:

tito_developer
July 3rd, 2010, 05:59 AM
I have facing the same problem. Is AS3 is really able to upload a file. Every one is discussing about the AS 330 codes but i really want to know the back-end PHP code where the uploaded files are really moved to a specific folder.

When we are using simple html form we use it this way

<input type='file' name='my_file'>

here the the field name is very important to catch the file in PHP. I want to know how to set the variable in AS3 with FileReference Object.

Please help me!

tito_developer
July 4th, 2010, 07:52 AM
Yes finally i have done it!
I read the reference of upload() method

I did not give any permission in the folder in server. Now its working well.
Thanks every one.;)

piyush_amica
August 27th, 2010, 07:39 AM
http://www.developphp.com/Flash_tutorials/show_tutorial.php?tid=6