Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
73
rated 0 times [  78] [ 5]  / answers: 1 / hits: 35980  / 11 Years ago, thu, may 23, 2013, 12:00:00

I have following code to read file through HTML5 File API. I have uploaded the file via
input=file element. Following is the code block.



<input type=file id=files name=file />
<button id=readFile>Read File</button>
<output id=content></output>

<script>

function readFile()
{
/* Get the reference of the inpout element. */
var files = document.getElementById('files').files;
console.log(files);

if (!files.length)
{
alert('Please select a file!');
return;
}

/* Reading the first file selected. You can process other files similarly in loop. */
var file = files[0];

/* Instantiate the File Reader object. */
var reader = new FileReader();

/* onLoad event is fired when the load completes. */
reader.onload = function(event) {
document.getElementById('content').textContent = event.target.result;
};

/* The readAsText method will read the file's data as a text string. By default the string is decoded as 'UTF-8'. */
reader.readAsText(file);
}

document.getElementById('readFile').addEventListener('click', function(event) {
readFile();
}, false);

</script>


What if I don't want to uploaded the file and provide filepath via input=type element to HTML5: File API to read the file and display it?



I know that the HTML5: File API don't take direct file path. Is there any solution ?


More From » jquery

 Answers
6

For Security reason, the browsers does not allow access to absolute path & file systems directly to Javascript. You can only get the file name by calling the 'val()' function in javascript and nothing more.



So don't waste your time.


[#78067] Wednesday, May 22, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
trayvon

Total Points: 35
Total Questions: 117
Total Answers: 88

Location: Guernsey
Member since Tue, Jul 6, 2021
3 Years ago
;