En esta página hay un ejemplo detallado para poder subir archivos a un servidor utilizando una interfase de flash : http://flash-db.com/Tutorials/upload/upFiles.php?page=2
Simple upload
Here's an excerpt of the file upload process:
//Allow this domain
System.security.allowDomain("http://localhost", "127.0.0.1");
import flash.net.FileReference;
// The listener object listens for FileReference events.
var listener:Object = new Object();
listener.onSelect = function(selectedFile:FileReference):Void {
statusArea.text += "Attempting to upload " + selectedFile.name + "\n";
selectedFile.upload("http://localhost/upload/upload.php");
};
// the file is starting to upload.
listener.onOpen = function(selectedFile:FileReference):Void {
statusArea.text += "Uploading " + selectedFile.name + "\n";
};
listener.onHTTPError = function(file:FileReference, httpError:Number):Void {
imagePane.contentPath = "error";
imagePane.content.errorMSG.text = "HTTPError number: "+httpError +"\nFile: "+ file.name;
}
listener.onIOError = function(file:FileReference):Void {
imagePane.contentPath = "error";
imagePane.content.errorMSG.text = "IOError: "+ file.name;
}
listener.onSecurityError = function(file:FileReference, errorString:String):Void {
imagePane.contentPath = "error";
imagePane.content.errorMSG.text = "SecurityError: "+SecurityError+"\nFile: "+ file.name;
}
// the file has uploaded
listener.onComplete = function(selectedFile:FileReference):Void {
statusArea.text += "Upload finished.\nNow downloading " + selectedFile.name + " to player\n";
details.text = ""
for(i in selectedFile) details.text +="<b>"+i+":</b> "+selectedFile[i]+"\n"
downloadImage(selectedFile.name);
};
var imageFile:FileReference = new FileReference();
imageFile.addListener(listener);
At the beginning, we allow the domain (in this case just localhost) trough System Security object to bypass sandbox violation messages (see Flash Player 8 security changes), next we setup some of the available listeners for our imageFile object. The most important are onSelect (a file was selected, so upload it) and onComplete, where the upload finish. The other handlers deals with other possible errors (HTTP, I/O, Security) and shows in the scrollPane we will use to display the loaded image (in the case of failure, of course) An important thing to take into account is the fact that onProgress event just return -1 for file upload, so you can't use a bar progress to know the progress of your uploading.
Note that in the onSelect handler we call the upload process just passing the path to the file in charge of managing the upload, a php file. You can use whatever server side language you want, as far as it can manage uploading. Remember to change path if your path is different.
In the next few lines we set the browse function, and finally show the image uploaded in the scrollPane
uploadBtn.onPress = uploadImage;
imagePane.addEventListener("complete", imageDownloaded);
// Call the uploadImage() function, opens a file browser dialog.
function uploadImage(event:Object):Void {
imageFile.browse([{description: "Image Files", extension: "*.jpg;*.gif;*.png"}]);
}
// If the image does not download, the event object's total property
// will equal -1. In that case, display am error message
function imageDownloaded(event:Object):Void {
if(event.total == -1) {
imagePane.contentPath = "error";
}
}
// show uploaded image in scrollPane
function downloadImage(file:Object):Void {
imagePane.contentPath = "http://localhost/upload/files/" + file;
}
Note that the browse method receives an array with an object with description and a list of allowed files. This is very useful to filter the files you need, and additionally difficult to select wrong ones. You can pass more than one list of files if you add in the array with proper description and extensions. The imageDownloaded function listen the complete event of our scrollPane, so in case the load fails we can show an alternate error message.
The PHP file
For those not not familiar with PHP (or other server side languages), good news:
move_uploaded_file($_FILES['Filedata']['tmp_name'], "./files/".$_FILES['Filedata']['name']);
chmod("./files/".$_FILES['Filedata']['name'], 0777);
This is our server file, just a command to move the file to a files directory keeping the same original file name. Remember to create the files folder in your server and give write/read permissions, or modify the path if you need to store the files in another location. The the script gives the file full permissons, because some servers are very restrictive on this. (also some server simple doesn't allow change permissons over uploaded files, consult your host provider)
Of course, you can perform (and is a must for production servers) more checking on the files, like: is the type of file I expect, is a legal file, etc? But this is outside the scope of our simple example and security mechanism will vary based on your server and language.
As you can see, not really difficult to manage, but a feature that many of us expect for a long time. If you are familiar with AS, is not difficult to follow the Help explanations to extend this simply example.
No comments:
Post a Comment