How could you use HTML to open a file on the client's machine as plaintext (i.e., a .cpp, .txt, or even a .html file)? I want to extract the plain textfile from the user's machine into an HTML <textarea>
. Just FYI, I am using hiccup, clojure, and webnoir to generate the HTML and server so those are all other options to use to help the process along.
Asked
Active
Viewed 712 times
0

RustyTheBoyRobot
- 5,891
- 4
- 36
- 55

Sam Mercier
- 193
- 1
- 7
-
Please clarify your question, this does not make any sense. Are you trying to open a file in Clojure and serve that as a webpage? or do html templating? – Istvan Devai Jun 28 '12 at 21:47
-
when you say `open a user side plaintext file`, do you mean that the file you want to open is on the client's machine? – jackwanders Jun 28 '12 at 21:52
-
I'm assuming this refers to reading the contents of a file on the server into a var in the webapp. – Arthur Ulfeldt Jun 28 '12 at 21:54
-
2perhaps this is about uploading a file? – Arthur Ulfeldt Jun 28 '12 at 21:57
-
Yes I mean I want to open the file on the user's machine. The file then become part of the web page. In a specific location where the text is extruded to that location/text box. – Sam Mercier Jun 29 '12 at 16:26
-
Actually it is a text area not a text box, but I think you get the idea. – Sam Mercier Jun 29 '12 at 17:39
2 Answers
1
You have two main options: upload the file to your server to be served as HTML content or use HTML 5's File API. This question also addresses a few more options (like applets, enabling drag-and-drop with the File API, etc.)
Upload the file
- Have the users choose the file using an
<input type="file" .../>
on one page - Upload the file contents to your server.
- Redirect to a different page to show the file contents
- Serve the uploaded file's contents in a textarea on that page.
Pros:
- This method is pretty simple and straightforward.
- You can scan the file and do some heavy processing on the server
Cons:
- Trips to the server can be time consuming.
HTML 5 Solution
- Have the user choose the file using an
<input type="file" .../>
- Instead of posting the contents, use JavaScript to load the file into your HTML 5 local storage (see code below)
- Use JavaScript to insert the contents of the file into your DOM.
Pros:
- No trip to the server (faster)
Cons:
- Any and all validation/processing must be done on the client side in JavaScript. This makes your client heavier and allows users to see/modify your code (which you might not need to care about).
I grabbed this code snippet from this site, which has some good examples of using the File API:
function onInitFs(fs) {
fs.root.getFile('log.txt', {}, function(fileEntry) {
// Get a File object representing the file,
// then use FileReader to read its contents.
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(e) {
// Do something with the contents, which are stored in 'this.result'
};
reader.readAsText(file);
}, errorHandler);
}, errorHandler);
}
window.requestFileSystem(window.TEMPORARY, 1024*1024, onInitFs, errorHandler);

Community
- 1
- 1

RustyTheBoyRobot
- 5,891
- 4
- 36
- 55
-
-
@SamMercier ...What do you mean? Are you trying to do this without hitting the server? – RustyTheBoyRobot Jun 29 '12 at 18:19
-
-
@SamMercier - After doing a little more research, I've updated my answer. – RustyTheBoyRobot Jul 02 '12 at 15:53
0
The quick and dirty option is to simply slurp
it:
(slurp "mycode.cpp")

Arthur Ulfeldt
- 90,827
- 27
- 201
- 284
-
I could slurp it but would that require the file to be known ahead of compilation and be server side or rather developer side? – Sam Mercier Jun 29 '12 at 16:37
-
Yeah, OP is asking about a file that resides on the client's machine. – RustyTheBoyRobot Jun 29 '12 at 18:12
-
so this answer only applies to server side files, not client side – Arthur Ulfeldt Jun 29 '12 at 18:43
-