How to display a text file using Javascript

J

jiayanxiang

Is there any sample code to use Javascript to load and display a text
file?

It will be best if the user can select files using some kind of
explorer. If that's complex, a text box to specify the file name will
be fine as well. Once a text file is selected, I just need to display
it in a text box.

Thanks for the help.
 
E

elan hung

Is there any sample code to use Javascript to load and display a text
file?
I'm not quite sure what your senario is. I assumed that you have make
your files accessible directly on web or through some sort of CGI
program. You have several ways to do the job and the easiest one is to
simply have them rendered in a frame/iframe with their respective urls.
It will be best if the user can select files using some kind of
explorer. If that's complex, a text box to specify the file name will
be fine as well. Once a text file is selected, I just need to display
it in a text box.

There's no such a thing as a 'explorer' in JavaScript which can be used
to browse server-side files. You have to design an 'explorer' page and
have a server program generating the file list.

The way I metioned above is to display your files in a frame/iframe. You
can css-style the frames to make them look exactly like a textbox (but
you will not be able to edit on them of course). If a textbox is really
required in your design, either you generate a page with file content
wrapped in textarea tag on the server-side, or on the client-side you
may consider using HTTPRequest object in JavaScript to fetch those files
and feed the text response to textarea element dynamically.

Hope this would help.


-- Elan Hung
 
D

Danny

Check around for Asynchronous JavaScript, aka Ajax, will be the way
to do it.

Danny
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top