clojure - Open a client's plain text file on an HTML page -
how use html open file on client's machine plaintext (i.e., .cpp, .txt, or .html file)? want extract plain textfile user's machine html <textarea>
. fyi, using hiccup, clojure, , webnoir generate html , server other options use process along.
you have 2 main options: upload file server served html content or use html 5's file api. this question addresses few more options (like applets, enabling drag-and-drop file api, etc.)
upload file
- have users choose file using
<input type="file" .../>
on 1 page - upload file contents server.
- redirect different page show file contents
- serve uploaded file's contents in textarea on page.
pros:
- this method pretty simple , straightforward.
- you can scan file , heavy processing on server
cons:
- trips server can time consuming.
html 5 solution
- have user choose file using
<input type="file" .../>
- instead of posting contents, use javascript load file html 5 local storage (see code below)
- use javascript insert contents of file dom.
pros:
- no trip server (faster)
cons:
- any , validation/processing must done on client side in javascript. makes client heavier , allows users see/modify code (which might not need care about).
i grabbed code snippet this site, has examples of using file api:
function oninitfs(fs) { fs.root.getfile('log.txt', {}, function(fileentry) { // file object representing file, // use filereader read contents. fileentry.file(function(file) { var reader = new filereader(); reader.onloadend = function(e) { // contents, stored in 'this.result' }; reader.readastext(file); }, errorhandler); }, errorhandler); } window.requestfilesystem(window.temporary, 1024*1024, oninitfs, errorhandler);
Comments
Post a Comment