Skip to content

Commit 6aff57b

Browse files
committedJan 8, 2012
Thoroughly comment code
1 parent 6662b81 commit 6aff57b

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed
 

‎README.md

+4
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ To run it, do:
88

99
And the application should be accessible at `http://localhost:9292`.
1010

11+
# What to look at?
12+
13+
The important bit is the client code, found at `/public/index.html`
14+
1115
If you want to know more, have a look at our [blog post covering HTML5 file uploads](http://blog.new-bamboo.co.uk/2010/7/30/html5-powered-ajax-file-uploads)

‎public/index.html

+57-4
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,83 @@
66
</head>
77
<body>
88

9+
<!--
10+
By default, we assume Ajax uploads are not supported.
11+
Later we'll detect support and change this message if found.
12+
-->
913
<p id="support-notice">Your browser does not support Ajax uploads :-(<br/>The form will be submitted as normal.</p>
1014

15+
<!-- The form starts -->
1116
<form action="/" method="post" enctype="multipart/form-data" id="form-id">
12-
<p><input id="file-id" type="file" name="our-file" /> <input type="button" value="Upload" id="upload-button-id" disabled="disabled" /></p>
17+
18+
<!-- The file to upload -->
19+
<p><input id="file-id" type="file" name="our-file" />
20+
21+
<!--
22+
Also by default, we disable the upload button.
23+
If Ajax uploads are supported we'll enable it.
24+
-->
25+
<input type="button" value="Upload" id="upload-button-id" disabled="disabled" /></p>
26+
27+
<!-- A different field, just for the sake of the example -->
1328
<p><label>Some other field: <input name="other-field" type="text" id="other-field-id" /></label></p>
29+
30+
<!-- And finally a submit button -->
1431
<p><input type="submit" value="Submit" /></p>
32+
1533
<script>
34+
// Function that will allow us to know if Ajax uploads are supported
1635
function supportAjaxUploadWithProgress() {
1736
return supportFileAPI() && supportAjaxUploadProgressEvents() && supportFormData();
1837

38+
// Is the File API supported?
1939
function supportFileAPI() {
2040
var fi = document.createElement('INPUT');
2141
fi.type = 'file';
2242
return 'files' in fi;
2343
};
2444

45+
// Are progress events supported?
2546
function supportAjaxUploadProgressEvents() {
2647
var xhr = new XMLHttpRequest();
2748
return !! (xhr && ('upload' in xhr) && ('onprogress' in xhr.upload));
2849
};
2950

51+
// Is FormData supported?
3052
function supportFormData() {
3153
return !! window.FormData;
3254
}
3355
}
3456

57+
// Actually confirm support
3558
if (supportAjaxUploadWithProgress()) {
59+
// Ajax uploads are supported!
60+
// Change the support message and enable the upload button
3661
var notice = document.getElementById('support-notice');
3762
var uploadBtn = document.getElementById('upload-button-id');
3863
notice.innerHTML = "Your browser supports HTML uploads. Go try me! :-)";
3964
uploadBtn.removeAttribute('disabled');
4065

66+
// Init the Ajax form submission
4167
initFullFormAjaxUpload();
68+
69+
// Init the single-field file upload
4270
initFileOnlyAjaxUpload();
4371
}
4472

4573
function initFullFormAjaxUpload() {
4674
var form = document.getElementById('form-id');
4775
form.onsubmit = function() {
76+
// FormData receives the whole form
4877
var formData = new FormData(form);
78+
79+
// We send the data where the form wanted
4980
var action = form.getAttribute('action');
5081

82+
// Code common to both variants
5183
sendXHRequest(formData, action);
5284

85+
// Avoid normal form submission
5386
return false;
5487
}
5588
}
@@ -58,43 +91,60 @@
5891
var uploadBtn = document.getElementById('upload-button-id');
5992
uploadBtn.onclick = function (evt) {
6093
var formData = new FormData();
94+
95+
// Since this is the file only, we send it to a specific location
6196
var action = '/upload';
97+
98+
// FormData only has the file
6299
var fileInput = document.getElementById('file-id');
63100
var file = fileInput.files[0];
64101
formData.append('our-file', file);
65102

103+
// Code common to both variants
66104
sendXHRequest(formData, action);
67-
68-
return false;
69105
}
70106
}
71107

108+
// Once the FormData instance is ready and we know
109+
// where to send the data, the code is the same
110+
// for both variants of this technique
72111
function sendXHRequest(formData, uri) {
112+
// Get an XMLHttpRequest instance
73113
var xhr = new XMLHttpRequest();
114+
115+
// Set up events
74116
xhr.upload.addEventListener('loadstart', onloadstartHandler, false);
75117
xhr.upload.addEventListener('progress', onprogressHandler, false);
76118
xhr.upload.addEventListener('load', onloadHandler, false);
77119
xhr.addEventListener('readystatechange', onreadystatechangeHandler, false);
120+
121+
// Set up request
78122
xhr.open('POST', uri, true);
123+
124+
// Fire!
79125
xhr.send(formData);
80126
}
81127

128+
// Handle the start of the transmission
82129
function onloadstartHandler(evt) {
83130
var div = document.getElementById('upload-status');
84131
div.innerHTML = 'Upload started!';
85132
}
86133

134+
// Handle the end of the transmission
87135
function onloadHandler(evt) {
88136
var div = document.getElementById('upload-status');
89137
div.innerHTML = 'Upload successful!';
90138
}
91139

140+
// Handle the progress
92141
function onprogressHandler(evt) {
93142
var div = document.getElementById('progress');
94143
var percent = evt.loaded/evt.total*100;
95144
div.innerHTML = 'Progress: ' + percent + '%';
96145
}
97-
146+
147+
// Handle the response from the server
98148
function onreadystatechangeHandler(evt) {
99149
var status = null;
100150

@@ -111,9 +161,12 @@
111161
}
112162
}
113163
</script>
164+
165+
<!-- Placeholders for messages set by event handlers -->
114166
<p id="upload-status"></p>
115167
<p id="progress"></p>
116168
<pre id="result"></pre>
169+
117170
</form>
118171
</body>
119172
</html>

0 commit comments

Comments
 (0)