File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Is it possible to write javascript to display files in textarea? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Is it possible to write javascript to display files in textarea?" Watch "Is it possible to write javascript to display files in textarea?" New topic
Author

Is it possible to write javascript to display files in textarea?

Amy Howard
Ranch Hand

Joined: Jul 11, 2002
Posts: 86
The example at http://www.faqts.com/knowledge_base/view.phtml/aid/1899/fid/130 can display files that are in the same directory in a textarea. But it doesn't work to display files that are in other directory. Please help me to get a solution to display client-side files in a textarea. Thanks!
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

You can browse your local filesystem like this:

/Rene


Regards, Rene Larsen
Dropbox Invite
Amy Howard
Ranch Hand

Joined: Jul 11, 2002
Posts: 86
Based on your suggestion, here is my try, but I still got a running error message "Permission denied" at the line downloader.startDownload(fileName, displayFile. Can you thy this, and let me know what the problem is? thanks!
<html xmlns:msie>
<msie ownload id="downloader" style="behavior:url(#default#download)" />
<HEAD>
<SCRIPT>
function fetchURL(url) {
if ((location.host == '' && url.indexOf(location.protocol) == -1)
||
url.indexOf(location.host) == -1)
{
netscape.security.PrivilegeManager.enablePrivilege
("UniversalConnect");
}
var dest = new java.net.URL(url);
var dis = new java.io.DataInputStream(dest.openStream());
var res = "";
while ((line = dis.readLine()) != null) {
res += line;
res += java.lang.System.getProperty("line.separator");
}
dis.close();
return res;
}
</SCRIPT>
<SCRIPT>
function loadFile (fileName) {
fileName = "file:///" + fileName.replace(/\\/g,'/');
if (document.layers) {
var i = new Image();
i.src = fileName;
var fileURL = i.src;
document.formName.file.value = fetchURL(fileURL);
}
else if (document.all && document.getElementById){
downloader.startDownload(fileName, displayFile);
}
}
function displayFile (text) {
document.formName.file.value = text;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="formName">
<SELECT NAME="files"
ONCHANGE="if (this.selectedIndex > 0)
loadFile(this.options[this.selectedIndex].value);"
>
<OPTION>-- Select a file to display --
<OPTION VALUE="C:\Temp\disclaimer.html">disclaimer.html
<OPTION VALUE="test.html">test.html
</SELECT>
<BR><BR>
<TEXTAREA NAME="file" ROWS="30" COLS="80" WRAP="off"></TEXTAREA>
</FORM>
</BODY>
</HTML>
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

You should disable the fileName formating

and use relative URL

/Rene
Amy Howard
Ranch Hand

Joined: Jul 11, 2002
Posts: 86
But how to display files in other directory/driver? This code doesn't work in this case.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

I don't know - I'm sorry.
/Rene
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
moving this to the javascript forum.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 413
If you want to do it in html page, there is a limitation - files should be in the same domain.
In NS you could "kind of" go over it with LiveConnect - but user will get a confirm dialog box.
If you want to use HTA files - you could load some files into hidden iframe and use innerHTML propertie, to get file content.
Also you could try to use FileSystemObject script object. Look at http://msdn.microsoft.com under Web Development/Scripting/Script runtime.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to write javascript to display files in textarea?