Having HTML document with Javascript code loaded from a local .html file (file:///C:/...
) is there any way in pure Javascript (no jQuery) to read local .txt file with all major browsers without adding any flag, such as --allow-file-access-from-files
in Chrome, etc.?
-
2First result on Google: html5rocks.com/en/tutorials/file/dndfiles– Waleed KhanCommented Jul 18, 2012 at 17:22
-
@Ωmega Of course it works with Chrome! I did not add any special flags.– NaftaliCommented Jul 18, 2012 at 17:26
-
2@Ωmega As a matter of fact, it is.– Waleed KhanCommented Jul 18, 2012 at 17:26
-
If you want to include IE as a "major browser", I'm sure you will need to use ActiveX.– BergiCommented Jul 18, 2012 at 17:30
Add a comment
|
1 Answer
You can do this only with HTML5
Example from that site:
<style>
.thumb {
height: 75px;
border: 1px solid #000;
margin: 10px 5px 0 0;
}
</style>
<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>
<script>
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Render thumbnail.
var span = document.createElement('span');
span.innerHTML = ['<img class="thumb" src="', e.target.result,
'" title="', escape(theFile.name), '"/>'].join('');
document.getElementById('list').insertBefore(span, null);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
-
1
-