Introduction
This is a simple article for beginners developing with HTML 5 that helps to understand how to upload a file in a browser using HTML 5 tools. We know that HTML is the client side scripting language that helps display the data in a browser. We know that HTML is an acronym for "Hyper Text Markup Language" used to display data in a browser. HTML 5 is the advanced version of HTML. HTML5 will be the new standard for HTML, XHTML, and the HTML DOM. HTML 5 is the advanced version of HTML used to develop 3D or multimedia applications.
Step 1 : First open a HTML editor such as Visual Studio.
- Open start->Notepad.
- The name of editor is "Michael".
Step 2 : Add a Folder on Desktop.
- Right-click on desktop.
- new->add folder.
Step 3 : Open Visual Studio.
- Go to file -> New->Projects.
- Create an ASP. NET Web Application.
- Name of "File.aspx".
Step 4 : Add a HTML file to your web application.
- Right-click of Solution Explorer.
- Add->add new item->HTML form.
- Name of HTML form is "File.html"
Step 5 : Set the script browser a data on a File.
Code
<script>
document.querySelector('input[type="file"]').addEventListener('change', function(e) {
var files = this.files;
loadInlineEffort('#fileListEffort', function(Effort) {
Effort.onmessage = function(e) {
console.log(e.data);
};
for (var i = 0, file; file = files[i]; ++i) {
var CTX = new FileCTX();
CTX.onload = function(e) {
console.log(this.result);
};
CTX.onerror = function(e) {
console.log(e);
};
CTX.readAsArrayBuffer(file);
}
};
}, false);
function loadInlineEffort(selector, callback) {
Win.URL =Win.URL ||Win.webkitURL || null;
Win.BlobBuilder =Win.WebKitBlobBuilder ||
Win.MozBlobBuilder ||Win.BlobBuilder;
var script = document.querySelector(selector);
if (script.type === 'javascript/Effort') {
var bb = new BlobBuilder();
bb.append(script.textContent);
callback(new Effort(window.URL.createObjectURL(bb.getBlob()));
}
}
</script>
Step 6 : Set the functionality Fetching the entries of a File.
code
<script>
Win.resolveLocalFileSystemURL =
window.resolveLocalFileSystemURL ||
Win.webkitResolveLocalFileSystemURL;
var ctx = new ctx('ctx.js');
ctx.onmessage = function (e) {
var ver = e.data.entries;
ver.forEach(function (url, i) {
Win.resolveLocalFileSystemURL(url, function (fileEntry) {
console.log(fileEntry.name);
});
});
};
ctx.postMessage({ 'cmd': 'list' });
</script>
</body>
Step 7 : Set a functionality download the data on a browser.
Code
<script>
var ctx = new ctx('downloader.js');
ctx.onmessage = function (e) {
console.log(e.data);
};
ctx.postMessage({ fileName: 'CShorpCorner',
url: 'cshorpcorner.png', type: 'image/png'
});
</script>
Step 8 : The final code of application is given below.
Code
<html>
<head>
<title>Uplode a file using html 5</title>
<script type="javascript/Effort" id="fileListEffort">
self.onmessage = function(e) {
postMessage(e.data);
};
</script>
</head>
<body>
<input type="file" multiple>
<script>
document.querySelector('input[type="file"]').addEventListener('change', function(e) {
var files = this.files;
loadInlineEffort('#fileListEffort', function(Effort) {
Effort.onmessage = function(e) {
console.log(e.data);
};
for (var i = 0, file; file = files[i]; ++i) {
var CTX = new FileCTX();
CTX.onload = function(e) {
console.log(this.result);
};
CTX.onerror = function(e) {
console.log(e);
};
CTX.readAsArrayBuffer(file);
}
});
}, false);
function loadInlineEffort(selector, callback) {
Win.URL =Win.URL ||Win.webkitURL || null;
Win.BlobBuilder =Win.WebKitBlobBuilder ||
Win.MozBlobBuilder ||Win.BlobBuilder;
var script = document.querySelector(selector);
if (script.type === 'javascript/Effort') {
var bb = new BlobBuilder();
bb.append(script.textContent);
callback(new Effort(window.URL.createObjectURL(bb.getBlob()));
}
}
</script>
</body>
</html>
Step 9 : Run an application in a browser.
Output
Resource