How can I get Flash to write data to an external file?
March 11, 2004 11:44 AM   Subscribe

How can I get Flash to write data to an external file? Is really the only decent method to create a server-side solution which does the actual work? Either way, pointers to examples would be much appreciated.
posted by warhol to Computers & Internet (11 answers total)
 
Having web applications (anything - not just flash) write to local files would be a major security problem. I don't think you'll find something that will do this. You could have the user install some custom ActiveX or something, but many users will be smart enough not to fall for that.

Mabye if we knew more about what you needed to do. Cookies can store values locally. In theory you could even store a half-assed flat-file database in a cookie.
posted by y6y6y6 at 11:52 AM on March 11, 2004


Response by poster: More isn't going to help, since it all boils down to flash writing some data to an external file. An example would be having an online flash game and saving high scores to a data file that can be later read. Another example would be having a flash front end to a database that a user modifies and then is able to store the changes back into the database.

You're right - it is a security concern and that's probably why flash doesn't do it natively - wanting to stay in the sandbox. However, it's easy to see where you could have flash make a call to something server-side, a jsp app for example, that would take the variables that flash passes to it that then writes to the local file.

I'm just trying to make sure that there's not some easy, elegant way to get to the endpoint that I'm currently overlooking. And then, further, trying to look at some examples to make sure that what I'm envisioning will work like I imagine it will.
posted by warhol at 12:33 PM on March 11, 2004


Serverside code can't write to a local file, either, except by literally downloading data to the user and saying "here, save this somewhere". At which point it becomes just another file on the user's computer, which is completely inaccessible to that serverside code. That's just the nature of the beast.
posted by ook at 12:51 PM on March 11, 2004


Response by poster: augh, mispoke. I'm thinking from the server point of view when I'm thinking of local file. My bad, my bad, not the user's machine, but the server. In my mind, from the flash app's point of view, "I'm being called from this server directory and I want to write to a little file in this same directory as me..."

Yeah, wrong frame of reference for this discussion.

So, flash app lives on a server, user calls it up in their browser, the flash app wants to write data to a text file on the server when the user saves changes.
posted by warhol at 12:56 PM on March 11, 2004


Oh, gotcha.

Well, I just realized that FlashMX can write to a local file (as in local to the user), in a limited sort of way: for that you want SharedObject.getLocal().

For storing data on the server side, as far as I know you do need a serverside script to catch and store the data, using sendAndLoad to submit form elements to it and/or receive the response.
posted by ook at 1:04 PM on March 11, 2004


I had this same question, specifically about how a Flash app or projector (locally) could copy a file from a CD-ROM to a local drive. What I think will work (haven't actually tried it, yet...) is using the fscommand "exec" command. I think you could use this to fire off an Applescript or shell command to copy a file (or write to a file).

This won't work out of a browser, though...
posted by jpburns at 1:28 PM on March 11, 2004


A good place to get answers to Flash ActionScript questions is the newsgroup macromedia.flash.actionscript
that is hosted on forums.macromedia.com.
posted by signal at 1:37 PM on March 11, 2004


When a Flash app hosted on a Web server requests an external file, does the IP address of that request come from localhost, the server's IP, or the IP of the user viewing the Flash app?

If it's either of the first two options, you could add a certain degree of security by only allowing queries to the server-side script from localhost or the server's IP, and ignore/deny all other requests. Ditto for referrers. Does Flash support any sort of encryption for data transmitted in this method? Can Flash access files outside of the web root?

IPs and referrers can, of course, be spoofed, but there's a question of whether someone would go through enough effort to spoof them and/or crack the encryption just to write a data file they can't see.
posted by Danelope at 1:56 PM on March 11, 2004


As ook mentions above, Local Shared Objects, while limited, do what you want.

Alternately, if Shockwave is an option for what you are doing, setPref will do something similar (again, in a very limited way), without popping a warning dialog.

Danelope: Flash usually runs in the user's browser, so the IP is the user's. Additionally, some popular browsers don't report referers with requests emitted from Flash.
posted by sad_otter at 3:08 PM on March 11, 2004


There's several ways of doing this. The flash movie cannot write to a text file on the server directly, you need to go through a server-side setup. I know ASP can read and write text files; i'm sure php can do the same.

The basic actionscript would be this type of function:

function sendhighscore(thescore) {
myVars=new LoadVars();
myVars.highScore=thescore;
myVars.send("testpage.asp");
}

with testpage.asp doing all the work. LoadVars defaults to POST for sending the data. Email me if you need more help.
posted by derbs at 3:26 PM on March 11, 2004


Can Flash do HTTP PUT calls, and use something like WebDav?
posted by namespan at 9:48 PM on March 11, 2004


« Older Crab Gone Bad?   |   Jaw Pain Newer »
This thread is closed to new comments.