-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml5AjaxFileUpload.js
63 lines (52 loc) · 2.66 KB
/
html5AjaxFileUpload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
HTML5 Ajax File uploading
Tested with ASP.NET MVC 4 (C#)
12/12/13 - github.com/code-for-coffee
idElementForFiles - name of the <input type="file"> id
idToAppendImagesTo - id of the <div>, <article>, etc to append image previews to
classForImagePreviews - CSS class for image previews
ajaxPostPath - Relative URL for where the Ajax POST should be directed to
idToPlaceResponseText - id of the <div>, <article>, etc to place the Ajax response text
Released under the MIT license.
*/
function html5AjaxUpload (idElementForFiles, idToAppendImagesTo, classForImagePreviews, ajaxPostPath, idToPlaceResponseText) {
var formdata = new FormData(); // Create FormData (HTML5 Required)
var uploadedFiles = document.getElementById(idElementForFiles); // set our uploadedFiles variable
var output = []; // array to store our image previews via HTML
// Iterate through each file in uploadedFiles
for (i = 0; i < uploadedFiles.files.length; i++) {
//Appending each file to FormData object
formdata.append(uploadedFiles.files[i].name, uploadedFiles.files[i]);
// regex of upload types that are allowed
var imageType = /image.*/;
// iterate through files that match the imageType regex
if (uploadedFiles.files[i].type.match(imageType)) {
var reader = new FileReader(); // Create FileReader (HTML5 Required)
if (uploadedFiles.files[i].type.match(imageType)) {
var img = document.createElement("img");
img.src = window.URL.createObjectURL(uploadedFiles.files[i]);
imageFilePath[i] = img.src;
img.classList.add(classForImagePreviews);
img.file = uploadedFiles.files[i];
document.getElementById(idToAppendImagesTo).appendChild(img);
reader.onload = (function (aImg) {
return function (e) {
aImg.src = e.target.result;
};
})(img);
reader.readAsDataURL(uploadedFiles.files[i]);
}
}
document.getElementById("list").innerHTML = output.join("");
}
// create new Ajax call and send
var xhr = new XMLHttpRequest();
xhr.open('POST', ajaxPostPath);
xhr.send(formdata);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
document.getElementById(idToPlaceResponseText).innerHTML = xhr.responseText;
}
}
return false;
}