views:

816

answers:

4

I've got a PHP-based site where we allow users to upload a comma-separated text file of data to be imported by some server side scripts. However, sometimes we need to adjust the column order of the data that is uploaded, and it would be immensely helpful if we could identify the columns in the CSV file before the upload takes place.

The only way I can think of to access local file I/O is with a Java plugin or an embedded Flash script, and I'd much prefer the latter. Is there any way in Flash to select a text file on the local hard drive, and then use ActionScript to examine only the first line (which corresponds to the column headings, in this example)?

Sometimes these files can be >100 MB, so loading the whole thing into memory and then throwing out all but the first line probably wouldn't work efficiently, so I'm wondering if there's syntax in ActionScript to limit the amount of data read in at a time.

+1  A: 

Flash movies don't have access to the local file system, except to do a file upload to the server.

I think your options are:

  1. Process the class on the client side with Java (trivial), using an applet. However, as you rightly assert the user would need to grant permissions to the applet.
  2. Provide a small client side application written with Adobe Air, using the file system API.
johnstok
+1  A: 

Couldn't you just use PHP to grab the first line of your CSV file and then feed that line of data to flash?

I could be completely wrong here but it looks like the person asking this question has done something similiar to that effect with their PHP code

http://stackoverflow.com/questions/173498/using-php-to-take-the-first-line-of-a-csv-file-and-create-a-mysql-table-with-th

vanhornRF
That would completely defeat the purpose. As I said, sometimes these files can be very large (>100MB), so I want this initial processing of the first line to take place **before** the upload.
SoaperGEM
A: 

The current version of the Flash player is only able to select a file for upload to a server, requiring all processing to be server-side. The next version of the player however, Flash 10 can read local files without making the round trip to a server.

I haven't used this functionality myself (yet) so I'm not sure if it's possible to read just a part of the file, but that does become less of an issue if it's all local.

Flash 10 is currently in some sort of prerelease beta, but it will roll out soon I think.

EDIT: Soon indeed, just saw this in my feed reader

grapefrukt
A: 

Flash Player 10, the current shipping version (just released 2 days ago), allows you to access local files. You can find an example here:

http://www.mikechambers.com/blog/2008/08/20/reading-and-writing-local-files-in-flash-player-10/

Hoewver, you have to load the entire file.

If you use a SWF running in Adobe AIR, then you can read just the first line (it has more advanced file APIs).

Best solution is probably to create a PHP API that returns just the first row of data.

mike chambers

[email protected]

mikechambers
I don't want to involve PHP with this at all. I will be uploading the file to PHP either way, but I wanted something client-side to process just the first row of data before the actual upload takes place, because sometimes the files can be quite large.
SoaperGEM