views:

121

answers:

3

Hi,

Is there any hook to react to a user drag and dropping a text file onto a browser? If the user drops a text file onto the browser with my page loaded, I'd like to be able to open the contents of the text file using javascript. I don't think this is possible, but just checking.

Is something like this possible in flash?

As an alternate, the user could specify a file path for me, then I could open the text file and read it. Seems like this is a security risk and probably won't be allowed, but just checking. It looks like this is possible with flash 10?

Thanks

+1  A: 

You can't read a local text file using Javascript because it violates the Browser security model.

Achilles
This is not entirely accurate, as you can use an AJAX call to attempt to open any resource via a URI... however, the browser will pitch a fit under default security settings. The user does have the option to allow this behavior if they so choose.
Josh
@Josh if the user does that they they will have larger problems than getting a txt file...
Achilles
True, but just wanted to let them know that it is not impossible. It's not a limitation of JavaScript, but the security that is enforced in the browser.
Josh
A: 

i think the last option is possible. maybe you can ask for a file path from javascript. you have some filepickers for this(google it)

then send te file-path to Flash by useing ExternalInterface or flashvars

and then handle the file-path as string in flash

see:

http://www.republicofcode.com/tutorials/flash/loading_external_text_bc/

michel
Modern browsers won't provide the complete file path from `<input type='file'>` elements. In other words, when you check the ".value" property on those elements, you don't get the complete (or real) pathname. I suspect you'd have to do the whole thing via Flash. Also, I don't think that link you provided has anything to do with loading content from *client* machines.
Pointy
no i tried it here a you only get the filename by javascript in firefox. IE only shows the filename. maybe you get it when you post or submit it in a form? you can use the method in the link if you upload it to your server, and if you want to use the SWF local you can use it also
michel
Again, for security reasons, the actual client machine pathname is not available to Javascript and not available to the server. All you can get reliably is the last part of the path (the file name, without any directories). IE7 probably gives you everything, but it's broken.
Pointy
you right i tried it on several ways. then you need to upload the document to the server and then access it from you SWF that is allready on the server...
michel
A: 

If you drag a text file (or an image, doc, etc) and drop it on an open browser, the browser will open the file for reading- like a document.write command, it wipes out the existing document.

kennebec