Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get/post -> filename -> file contents

Status
Not open for further replies.

Leozack

MIS
Oct 25, 2002
867
GB
I struggled with this half a year ago ([URL unfurl="true"]http://www.tek-tips.com/viewthread.cfm?qid=1239743&page=2[/url]) and everytime I come back to it I can't do it so I'm posting again. kennethkawamoto said before :
Code:
You can do like this:

myVars = new LoadVars();
myVars.load("config.txt");
myVars.onLoad = function(success) {
    if (success) {
        doSometing()
    } else {
        trace(" Error loading config.txt ");
    }
};
//
function doSomething() {
    //Do something
}
//
stop();
//

function doSomething() will not be executed until myVars is loaded with data.

But I'm struggling still. So I'll simplify my arrangement :

Say I have a config file called "testfile.txt" and it resides in a folder called "configfiles" which is a folder on my local http server. For arguments sake at the root so To get the information from inside the config file into flash (that information would be links to pictures I'd want to load etc)I planned on being able to pass the swf a config filename (testfile.txt) either by GET myflash.swf?fn=testfile or by post (embedded in the html).

The flash checks for either of those possible inputs (and if both exist, POST overides). It then loads that config file and extracts image links from within it to load.

I've tried using the previous techniques to just read in some simple &this=that variables from a test file, but I just can't seem to get them into variables and then usable in the script from then on. I'm also somewhat confused on :
- can I call a file relatively? eg just testfile not path/to/testfile?

- will such things work using the .fla/.swf project path as it's starting (relative) directory with the preview movie funtion? Or mustI keep exporting to swf and moving that to my apache root and running it from my webserver and using localhost paths etc?

I must admit, this seems adventurous of me whenI could've got most of this working in some other form by now, but I wanted to really try to use flash o.o

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
PS - I'm using flash mx 6.
PPS - when I refer to "can I use relative paths in the script for loading files or images" I mean lik eimages in html, where you could just say "images/image.jpg", aka, in flash could I just say myVars.load("config.txt") if the swf and config.txt are in the same folder, or myVars.load("config/files/config.txt") if the config file is in a subfolder files of a subfolder config, to teh swf file's location?

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
You can use relative paths e.g. [tt]myVars.load("files/config.txt");[/tt]

The script you posted won't work as there's a typo - "doSometing()" should be "doSomething()". (May it was me who mistyped in the first place!)

Kenneth Kawamoto
 
Ok well I tried this swf file
Code:
/* first create a new instance of the LoadVars object */ 
myVars = new LoadVars(); 

// call the load method to load my php page 
myVars.load("test/testfile.txt");

myVars.onLoad = function(success) {
    if (success) {
        doSomething();
    } else {
        trace(" Error loading .txt ");
    }
}
//
function doSomething() {
	textbox.text = myVars.test1;
	textbox2.text = myVars.test2;
	textbox.text = "hmm "+myVars.test1;
}

stop();
on my local apache running in a folder with a 'test' subfolder containing testfile.txt which contains
Code:
&test1=test1baby
&test2=test2baby!
On the actual flash page I just have 2 textboxes with textbox and textbox2 in but set to be dynamic text with those names. They should get updated with values from the file if it read in properly.
I seem to be having such huge problems just trying to read stuff in, next comes the get/post grabbing (and choice) etc x_x

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Well I copy & pasted your script and the TextFields say "hmm test1baby" and "test2baby!" as expected.

May be you put your SWFs in a sub directory? If so the relative path changes its base to the hosting HTML.

Kenneth Kawamoto
 
ok I moved the .swf upto the root directory and kept the textfile in a 'test' subdirectory and tried that, but still no luck when visiting :(

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
I just tried it on my root HD folder and used IE to open D:\vartest2.swf and it still just displayed "textbox2" etc :( any chance of sending me the .fla/.txt you have so I can try with that?

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
I tried the standalone palyer too ;P and I also have FF, neither makes a difference :/

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Interesting indeed! I get this

test2=goodbye&test1=hello&onLoad=%5Btype%20Function%5D

when the textfile contains just

&test1=hello&test2=goodbye

and while I'm here, can you start the textfile without the first &? just a & for each new var, and newlines after each if you want?

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
The trace is good, "onLoad" should be there as myVars object has the onLoad Function.

Now we can tell the problem is actually your TextFields.

What do you get if you put these trace "trace(textbox);" and "trace(textbox.text);" in doSomething() function?

As for the first "&" in the text file, I personally never put it.

Kenneth Kawamoto
 
I moved the trace(this); from the if(success) down to the doSomething() function that is called from if(success), and the trace now says just
_level0
...
I tried the trace(textbox) stuff and if I put it in the if(success) then textbox shows the original text and text.box shows "undefined". Moving them down to doSomething() function makes no difference though?
/confused

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
trace(this) will say "_level0" if you put it in the doSomething() as the Function doSomething is defined in _level0 (_root). But that's not what I meant.

What I meant was to put "trace(textbox)" in doSomething(), which you get "undefined". This means you don't have the TextField instance called "textbox" on Stage. That's why your script isn't working.

Name your TextField as "textbox" and your movie should work.

Kenneth Kawamoto
 
ah <_<'
I had the 'var' field for the textboxes as textbox and testbox2 but not the 'instance name' which I set to be those too.
I now get
_level0.textbox2
But iirc I could manage to load the vars in at that point but wasn't able to store them somewhere that was accessible to the rest of the script? Like a global variable? You can tell it's been a long time since I did any actionscript, too much C/php ...

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Assuming "anywhere" is in the doSomething() function? Anything outside of that wouldn't?
If so then ok, now I just have to get the name/path to the file to be read in from GET/POST, then work out how to use image url's (the var's I'm importing) in the flash etc ... /phew

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Anywhere means anywhere, not just doSomething function but anywhere. This is because myVars is initially created in the main timeline (_level0/_root) and you can access to the main timeline from anywhere.

Kenneth Kawamoto
 
Well the following doesn't work because I moved them outside of doSomething()
Code:
myVars = new LoadVars(); 

// call the load method to load my php page 
myVars.load("test/testfile.txt");
//myVars.load("testfile.txt");

myVars.onLoad = function(success) {
    if (success) {
		//trace(this);
		//trace(textbox2);
        doSomething();
    } else {
        trace(" Error loading config.txt ");
    }
}
//
function doSomething() {
	//trace(textbox2);
	//trace(this);
	//textbox.text = myVars.test1;
	//textbox2.text = myVars.test2;
	//textbox.text = "hmm "+myVars.test1;
}

	textbox2.text = myVars.test2;
	textbox.text = "hmm "+myVars.test1;

stop();

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top