Jump to content

reading from / writing to files


fuchs63

Recommended Posts

Simple question... I am a bit confused with the "input type=file" in a form.Does that mean I can access e. g. a txt-file on the computer, load it's contents and do wild things with it? There is no method actually accessing the contents of a file, at least I haven't recognized any. So the question is: Can I e. g. have a list of expenses in a txt-file, access that file, calculate the overall sum of expenses, and write the result back to the document (or possibly even another file)? If so, how do I do that?Thanks a lot! Fuchs

Link to comment
Share on other sites

type="file" is generally used for uploading files to a server (as when you upload a photo to facebook or add an attachment to a webmail client).I don't remember how to do it anymore, but I think you can access files on your desktop from within your webpage. The limitation is that your webpage has to be located on your desktop also. This is a security measure designed to ensure that you can access only your own documents.And if you're going to do that, you might as well use a gui that's designed for standalone use.

Link to comment
Share on other sites

type="file" is generally used for uploading files to a server (as when you upload a photo to facebook or add an attachment to a webmail client)....I think you can access files on your desktop from within your webpage. The limitation is that your webpage has to be located on your desktop also. This is a security measure designed to ensure that you can access only your own documents.
Well, that's fine as the website I intend to create is indeed meant for use as standalone application. I have devised a Java-Script application that checks a given (special-purpose) text for compliance with certain rules that the text must comply to... such as order of entries, spell-checking of technical expressions and the like. Thus far I copy and paste that text in a form, and I just want to get rid of that process by accessing the txt-file directly. Instead of copying/pasting I would rather press a button on the site, which then would open the file, read the content, check it and come back with any error messages. I agree it would be far easier to do that directly at the source e. g. with VBA (the examined text is produced in Word), however I am not allowed to run my personal Macros on a company computer for security reasons. Hence the workaround with JavaScript (the network admin had no objections against that). So, if anyone could show me how to access a locally stored file and access the contents, I would be very thankful! RegsFuchs
Link to comment
Share on other sites

This isn't styled to look nice, and I hate using an iframe, but it works--but only as long as the HTML doc and the file you want to open are in the same folder. If there's a way to access the complete path of the file, I don't know it. Or I seem to recall the value is different depending on the browser . . . ? I don't feel like checking. Maybe you could play with it :)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"   "http://www.w3.org/TR/html4/strict.dtd"><html>	<head>		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">		<title></title>		<script type="text/javascript">			// THESE TWO LINES DO ALL THE WORK			document.getElementById("loadit").onclick = function () {				document.getElementById("myframe").src = document.getElementById("fname").value;			}			window.onload = init;		</script>	</head>	<body>		<div>			<iframe id="myframe"></iframe>			<input type="file" id="fname"><button id="loadit">Load file</button>		</div>	</body></html>

Link to comment
Share on other sites

I like ajax much better than frames, so use this this to load text files,

function IO(U, V) {    var X = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();    X.open(V ? 'PUT' : 'GET', U, !1);    X.setRequestHeader('Content-Type', 'text/html')    X.send(V ? V : '');return X.responseText;}

example: alert(IO("text.txt")) would display the contents of a file "text.txt" in the same folder as the page.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...