Skip to content

Instantly share code, notes, and snippets.

@Harshapriya123
Created March 5, 2019 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Harshapriya123/b70772510def676616bf1c6d9e5d8c52 to your computer and use it in GitHub Desktop.
Save Harshapriya123/b70772510def676616bf1c6d9e5d8c52 to your computer and use it in GitHub Desktop.
1. Create a page that has a button(that says "Upload file") to "upload" a file(preferably a text file is enough) from the user's computer. 2. Modify the content of file(Let's say the content of file was "Good Morning", change to "Good Evening"). 3. Display the previous content and new content on the page again only when another button(that says …
<!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=UTF-8' >
<script>
function startRead()
{
// obtain input element through DOM
var file = document.getElementById('file').files[0];
if(file)
{
getAsText(file);
}
}
function getAsText(readFile)
{
var reader;
try
{
reader = new FileReader();
}catch(e)
{
document.getElementById('output').innerHTML =
"Error: seems File API is not supported on your browser";
return;
}
// Read file into memory as UTF-8
reader.readAsText(readFile, "UTF-8");
// Handle progress, success, and errors
reader.onprogress = updateProgress;
reader.onload = loaded;
reader.onerror = errorHandler;
}
function updateProgress(evt)
{
if (evt.lengthComputable)
{
// evt.loaded and evt.total are ProgressEvent properties
var loaded = (evt.loaded / evt.total);
if (loaded < 1)
{
// Increase the prog bar length
// style.width = (loaded * 200) + "px";
document.getElementById("bar").style.width = (loaded*100) + "%";
}
}
}
function loaded(evt)
{
// Obtain the read file data
var fileString = evt.target.result;
document.getElementById('output').innerHTML = fileString;
document.getElementById("bar").style.width = 100 + "%";
}
function errorHandler(evt)
{
if(evt.target.error.code == evt.target.error.NOT_READABLE_ERR)
{
// The file could not be read
document.getElementById('output').innerHTML = "Error reading file..."
}
}
</script>
</head>
<body>
<input id="file" type="file" multiple onchange="startRead()">
<h3>Progress:</h3>
<div style="width:100%;height:20px;border:1px solid black;">
<div id="bar" style="background-color:#45F;width:0px;height:20px;"></div>
</div>
<button type="button">File Contents</button>
<pre>
<code id="output">
</code>
</pre>
</body>
</html>
@Harshapriya123
Copy link
Author

I meant to modify the content of file in “code” and display the initial content of file and new content of file which is not done yet.
You can create a separate input element for the replacement text or just hard-code the replacement text.

But, most important is that some pieces of code are unnecessarily/incorrectly used.
Please let me know if you have any queries.

@Harshapriya123
Copy link
Author

Show contents on click of the button the contents has to show as old and new contents

@Harshapriya123
Copy link
Author

  1. Create a page that has a button(that says "Upload file") to "upload" a file(preferably a text file is enough) from the user's computer.
  2. Modify the content of file(Let's say the content of file was "Good Morning", change to "Good Evening").
  3. Display the previous content and new content on the page again only when another button(that says "Show uploaded content") is clicked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment