FileReference:Class
The Flash FileReference Class is a tool for uploading files by:
- Popping up a browse box, prompting the user to define the file they would like to upload
- Uploading the required file, using either php, coldfusion, or asp.
- Keeping Flash up to date on the progress of uploading the file.
- And returning to Flash either an error or else it notifies Flash that it uploading has been completed successfully.
The properties of the FileReference :Class are as follows:
- addListener(listener:Object) :Function
- browse(typelist:Array) :Function
- cancel:Function
- creationDate:Date[read-only]
- creator:String[read-only]
- download
- modificationDate:Date[read-only]
- name:String[read-only]
- onCancel:Function -runs when user cancels
- onComplete:Function -runs when uploading completes
- onHTTPError:Function -runs when a HTTPError is encountered
- onIOError:Function -runs when an IOError is encountered
- onOpen:Function -runs when file is Opened
- onProgress:Function -runs when upload progress is made
- onSecurityError:Function -runs when a security error is encountered
- onSelect:Function -runs when user selects which file to upload
- removeListener:Function
- size:Number[read-only]
- type:String[read-only]
- upload:Function
Judging by the number of people having problems with the FileReference class, a list of causes of errors would be helpful. If an error you are currently having is not listed, then please feel free to comment on it.
- 406 error
- This might be able to be fixed by going to the root directory on your site, and adding the following lines to .htaccess (if it is not there, then create it)
- SecFilterEngine Off
- SecFilterScanPOST Off
- or, on the other hand, if your page is under a secure connection,
Flash cannot upload under a secure connection, so if you are trying for example to upload to https://mysite.com/upload.php, it won’t work, because flash cannot upload under a secure connection, however, you can upload to http://mysite.com/upload.php - or a combination of both
- This might be able to be fixed by going to the root directory on your site, and adding the following lines to .htaccess (if it is not there, then create it)
- If you would like to have an error listed, please comment on your error, and an administrator may add it to the blog
Also included in this blog is a list of other articles on the FileReference Class and it’s errors
The code for php is:
$fileName = basename($_FILES[”Filedata”][”name”]);
$uploadFile = “images/uploaded/” . $_GET[”date”] . “-” . $fileName;if (move_uploaded_file($_FILES[”Filedata”][”tmp_name”], $uploadFile)) {
chmod(dirname(__FILE__) . “/” . $uploadFile, 0777);
print 1;
}else{
print 0;
}
?>
The code for coldfusion:
The code for asp:
<%@LANGUAGE=”VBSCRIPT” CODEPAGE=”1252″%>
<%Set theForm = Server.CreateObject(”ABCUpload4.XForm”)
theForm.Overwrite = True ‘ overwrite existing files
.
For Each theFile in theForm.Files
If theFile.FileExists Then
‘ save in virtual directory
theFile.Save “temp\” & theFile.FileName
Response.Write(”1 ” & theFile.FileName)
Else
Response.Write(”0″)
End If
NextSet theForm = nothing
%>
If you have any comments or questions please feel free to let us know… and
Thanks for reading
…Also, a tutorial on this is coming soon!
June 11th, 2007 at 8:37 pm
I am always experiencing the I/O Errors.
It seems when the connection status is bad (as a result from pinging to server) , the I/O error triggers.
Do u have any good idea for handle with this kind of I/O Errors. As any idea for supervention-uploading after I/O error….
June 11th, 2007 at 8:46 pm
So it works some of the time,
yet other times “when the connection status is bad”,
it does not work at all?
Would you mind posting the code for uploading?
June 11th, 2007 at 8:53 pm
Also, how far does it get in the upload process, in bytes or on percent?
June 16th, 2007 at 4:48 am
I’m getting HTTP 500 errors.
I’m using the FileReference class to upload images to an aspx page. Basically it goes like this:
Flash uploads the file to http://www.mydomain/upload.aspx
The aspx page calls a webservice that sends the image to another server (my image server). I’m of course not sending it directly to the image server because Flash can’t do that (it’s on another server)
It looks like Flash is successfully uploading the image (100%) and then I get the HTTP 500 error. It seems to work when I’m testing locally, but once the .swf goes live on the server, no go. The .swf and the .aspx page are in the same directory.
I’d also like to know more about the 500 error. w3.org says the following, but it’s still so vague:
10.5.1 500 Internal Server Error
The server encountered an unexpected condition which prevented it from fulfilling the request.
Please assist!!
June 16th, 2007 at 9:32 am
Just wondering what code you are using for your upload
Also, are you making sure to allow localhost and whatever other sites you have to access
for instance:
System.security.allowDomain(”http://localhost”, “127.0.0.1″);
June 16th, 2007 at 9:45 am
awflasher:
your problem is caused by uploading a large file, and the resetting of connection happens too often to allow the whole file to upload
You can fix this by finding php.ini on your server, and editing it or if you do not have any php.ini on your site, go to this link and find out how to create one.
http://www.washington.edu/computing/web/publishing/php-ini.html#register_globals
June 16th, 2007 at 10:10 am
wraevn:
Just thinking that maybe the reason that flash is having the errors is something to do with calling another service(”The aspx page calls a webservice that sends the image to another server (my image server).”)
Have you made sure that flash didn’t actually upload the image, even though it got the error, it may still have uploaded it?
June 17th, 2007 at 1:59 am
Well, it’s all figure out … it turned out that the upload WAS working, in that the image was making it to the image server, but the image server (running DotImage by Atalasoft) was at v. 5.0, and the website .dlls were published using v. 3.0. In addition, it was trying to convert an image format to the same image format and was throwing an exception for that.
My AS was perfect
It just shows to go ya!
However, your comments are all valid, and are definitely things I would’ve looked into should the problem have been on my side. The Security is a big one and all that stuff is being set properly, so good call there.
August 7th, 2007 at 7:15 am
Hi,
I’ve got a problem with an upload, using php and the as.
My file is downloading to 100% but on firefox mac I’ve got an 404 error and on firefox pc got a 406 error…
I’ve tried the secfilterengine and other in .htaccess but it doesn’t work and crash the server…
Somebody can help me???
Ben.
August 7th, 2007 at 8:32 am
Hi Ben,
Just wondering if your page is under a secure connection. ie. is it at https: or http:
Also, if you would like to post the code you are using I can look through that.
Cheers,
Protoflash
August 7th, 2007 at 8:38 am
Hi thank you very much…
My website is in http://
and the code for the php is:
and for the as:
var domain:String = “www.mydomain.com”;
var url:String = domain + “/test/admin/”;
//Allow this domain
System.security.allowDomain(domain);
import flash.net.FileReference;
upload_button.button_text.text = _root.title;
// The listener object listens for FileReference events.
var listener:Object = new Object();
loading._x = Stage.width - loading._width;
var screen:Object = new Object();
screen.onResize = function(){
loading._x = Stage.width - loading._width;
}
Stage.addListener(screen);
loading._visible = false;
listener.onSelect = function(selectedFile:FileReference):Void {
selectedFile.upload(url + “includes/uploadFile.php?upload_dir=” + _root.upload_dir);
loading._visible = true;
file_name.text = “”;
}
upload_button.loading._xscale = 0;
listener.onProgress = function(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
upload_button.loading._xscale = bytesLoaded / bytesTotal * 100;
file_name.text = Math.round(upload_button.loading._xscale) + “% - “+bytesLoaded+” bytes of “+bytesTotal+” total”;
}
listener.onOpen = function(selectedFile:FileReference):Void {
}
/* ERRORS */
listener.onHTTPError = function(file:FileReference, httpError:Number):Void { file_name.text = “http error: ” + httpError + ” dir: ” + _root.upload_dir; }
listener.onIOError = function(file:FileReference):Void { file_name.text = “IO error”; }
listener.onSecurityError = function(file:FileReference, errorString:String):Void { file_name.text = ” security error: ” + errorString; }
function onLoadInit(){
}
loader = new MovieClipLoader();
loader.addListener(this);
loader2 = new MovieClipLoader();
listener.onComplete = function(selectedFile:FileReference):Void {
loading._visible = false;
upload_button.loading._xscale = 0;
if(_root.hidden){
getURL(”javascript:setHidden(’”+_root.hidden+”‘, ‘”+ selectedFile.name+”‘, ‘” + _root.upload_dir + “‘);”);
}else{
getURL(”javascript:setImage(’”+_root.image_tag+”‘, ‘”+ selectedFile.name+”‘, ‘” + _root.upload_dir + “‘);”);
}
file_name.text = selectedFile.name;
_root.remove_visible = “true”;
}
if( _root.image_name == “” ){
_root.remove_visible = “false”;
file_name.text = “”;
} else {
_root.remove_visible = “true”;
file_name.text = _root.image_name;
}
remove.onEnterFrame = function(){
if( _root.remove_visible == “false” ){
this._visible = false;
//file_name.text = “”;
} else {
this._visible = true;
//file_name.text = _root.image_name;
}
}
remove.onRelease = function(){
file_name.text = “”;
if(_root.hidden){
getURL(”javascript:setHidden(’”+_root.hidden+”‘, ”, ”);”);
}else{
getURL(”javascript:setImage(’”+_root.image_tag+”‘, ”);”);
}
_root.remove_visible = “false”;
}
var imageFile:FileReference = new FileReference();
imageFile.addListener(listener);
upload_button.onPress = function(event:Object):Void {
//imageFile.browse([{description: “Images”, extension: “*.jpg;*.gif;*.png;*.jpeg”}, {description: “Documents”, extension: “*.ppt;*.zip;*.rar;*.ai;*.psd;*.doc;*.xls;*.flv;*.eps;*.pdf”}]);
imageFile.browse([{description: “Documents”, extension: “*.jpg;*.gif;*.png;*.jpeg;*.pdf;*.doc;*.xls;*.ppt;*.pps”}]);
}
Thanks in advance…
Ben.
August 7th, 2007 at 9:35 am
Just with a quick look at your code. Try using
System.security.allowDomain(”http://localhost”, “127.0.0.1″);
instead of System.security.allowDomain(domain);
You may get better results this way.
August 7th, 2007 at 7:32 pm
Hi,
but this is on a website…
It’s not in local…
Where do I put my I.P. for the server?
Thanks in advance,
Ben.
August 7th, 2007 at 7:45 pm
Hi,
Allowing Localhost will allow the actual site.
August 7th, 2007 at 7:55 pm
Hi,
okok, I can see it’s close to work.
The loading goes to 100% and write the name of the image… but the copy doesn’t work…
Do you have an idea about it???
Thanks again!!!
Ben.
August 7th, 2007 at 8:05 pm
Hi,
I’ve find my error:
POST instead of GET in the php…
Thank you very much for everything…
Ben.
August 7th, 2007 at 8:38 pm
No worries, mate. I’m just sorry I didn’t think of that.
btw. I am a flash freelancer, so if you have any work you would like to pass on to us please feel free to contact me at protoflash.com | at | gmail.com
Cheers,
Protoflash
January 30th, 2008 at 7:06 am
I am trying to upload a file using rails server side script. …. it is working in safari but in IE and FF i get 406 error in server log…
Some ideas ?
January 30th, 2008 at 8:48 am
Try creating a .htaccess file in your root directory with:
SecFilterEngine Off
SecFilterScanPOST Off
If that doesn’t work, move the .htaccess file to the directory of the .swf file.
This might do the trick…
If it doesn’t, do you know what percentage the upload is getting to before it comes across the error?
Can you possibly give me a link so I can see exactly what is happening?
February 8th, 2008 at 1:47 am
Re: “Flash cannot upload under a secure connection”…
I have a secure server and I can upload just fine using Flash / PHP over https… but it won’t with FireFox and apparently it is related to a .htaccess setting (which is what led me to this thread). IE6 & IE7 can and do upload over a secure connection… so maybe I’m doing something different? Who knows.
February 8th, 2008 at 8:07 am
Try uploading over http:, that might do the trick….
February 12th, 2008 at 2:41 pm
I have my flash 8 (AS 2.0) upload working via fileReference.upload using a coldfusion upload.cfm page.
The problem is that I can upload small files but larger ones such as a 1 meg file upload to 100 % but return with an error code 500 and fails to transfer over http.
February 12th, 2008 at 3:01 pm
500: This error could be due to many things, but it is an “Internal server error”. Issues to check:
- You are uploading files larger than the allowed post size set in the CF administrator. See the setting “Maximum size of post data” in the Settings page of the CF administrator. The default I think is 100MB
- You are uploading large files and CF is running out of memory. CF has to put the whole size in memory while it gets uploaded before it saves it to disk. If you have access to the logs (the java output logs, not the regular cf logs), check for this error.
http://www.asfusion.com/blog/entry/file-upload-with-coldfusion-flash-forms
Let me know how it goes…
March 4th, 2008 at 10:19 am
500 error here as well. I’ve tried everything- googled myself into the ground. htaccess file in root and swf folder breaks the server. Tried the domain security (AS) patches, and even the cross domain file, still no luck. Just gets to 100%, and throws the 500 error. The same exact script (I used for ever) works on all my other servers, but of course, not the one my clients are using. Tried every folder permission combo also - 703, 704, 777, etc etc etc. Godaddy won’t help - to them, this is a “scripting” issue, even though the same exact script works on a neighboring server.
April 16th, 2008 at 6:40 pm
Hi,
I’m looking for ages for an explanation of this sentence in Flash doc :
“Although Flash Player has no restriction on the size of files you can upload or download, the player officially supports uploads or downloads of up to 100 MB”
Why ‘officialy support 100MB’ ? what could happen if I upload 101MB or 200MB ?
thanks for any hint or links
April 17th, 2008 at 6:19 am
Hi William,
I’v got a hunch that it’s because the .htaccess files on the server, etc. stop it from uploading more than 100MB normally, because the page times out. So I think you’d have to modify them or look for some other server-side settings.
Other than that, I can’t think of anything to suggest except maybe try it!
Cheers,
Stephen