PDA

View Full Version : Browse files..


Scotlad
01-08-2007, 12:45 PM
Is it possible to allow the user to click a button on a flash app to open a dialogue box and browse their local drive for files? Is it in any way possible? All i want is them to be able to browse through and choose one and the name of that file to be stored as a variable..

mcmcom
01-08-2007, 03:40 PM
this article shows you how to use FileReference to get a file from the users local hard drive. Note that this only gets the file info, you need Server Side code to actually upload anything

http://www.peachpit.com/articles/article.asp?p=464427&rl=1

hth,
mcm

Scotlad
01-08-2007, 04:33 PM
Thanks for that mcmcom thats fantastic.

dialectric
04-01-2007, 10:24 PM
I think there must be a way to use the built in browse dialog box found in FileReference without a server. This would entail running FileReference without uploading anything, then taking the path that the user selected, and pushing it to loadvars. (or does loadvars only load files in the same directory as the flash movie?)

I can get the file name they've picked, just not the path.

//Allow this domain
System.security.allowDomain("http://localhost/");
import flash.net.FileReference;

var listener:Object = new Object(); // The listener object listens for FileReference events.
listener.onSelect = function(selectedFile:FileReference):Void {
statusArea.text = details.text = ""
statusArea.text += "Attempting to upload " + selectedFile.name + "\n";
trace ("selected file : " +selectedFile);
trace ("selected file name: " +selectedFile.name);
//my trace to get the path would be here, saving it to _root level var

// old code to call php
//selectedFile.upload("upload.php");

};

// the file is starting to upload.
listener.onOpen = function(selectedFile:FileReference):Void {
statusArea.text += "Uploading " + selectedFile.name + "\n";
};

//Possible file upload errors
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 {
// Notify the user that Flash is starting to download the image.
statusArea.text += "Upload finished.\nNow downloading " + selectedFile.name + " to player\n";
//Show file details
details.text = ""
for(i in selectedFile) details.text +="<b>"+i+":</b> "+selectedFile[i]+"\n"
// Call the custom downloadImage() function.
downloadImage(selectedFile.name);
};

var imageFile:FileReference = new FileReference();
imageFile.addListener(listener);

//trigger - now called by main movie
//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;*.txt"}]);
}

// 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 = "./files/" + file;
}

//stop()