Last active
April 24, 2021 03:53
-
-
Save aponxi/f279798b2850d7a065c305dd34d61db7 to your computer and use it in GitHub Desktop.
File Upload Pure JavaScript https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Sending_forms_through_JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Because we want to access DOM node, | |
// we initialize our script at page load. | |
window.addEventListener('load', function () { | |
// This variables will be used to store the form data | |
var text = document.getElementById("i1");; | |
var file = { | |
dom : document.getElementById("i2"), | |
binary : null | |
}; | |
// We use the FileReader API to access our file content | |
var reader = new FileReader(); | |
// Because de FileReader API is asynchronous, we need | |
// to store it's result when it has finish to read the file | |
reader.addEventListener("load", function () { | |
file.binary = reader.result; | |
}); | |
// At page load, if a file is already selected, we read it. | |
if(file.dom.files[0]) { | |
reader.readAsBinaryString(file.dom.files[0]); | |
} | |
// However, we will read the file once the user selected it. | |
file.dom.addEventListener("change", function () { | |
if(reader.readyState === FileReader.LOADING) { | |
reader.abort(); | |
} | |
reader.readAsBinaryString(file.dom.files[0]); | |
}); | |
// The sendData function is our main function | |
function sendData() { | |
// At first, if there is a file selected, we have to wait it is read | |
// If it is not, we delay the execution of the function | |
if(!file.binary && file.dom.files.length > 0) { | |
setTimeout(sendData, 10); | |
return; | |
} | |
// To construct our multipart form data request, | |
// We need an XMLHttpRequest instance | |
var XHR = new XMLHttpRequest(); | |
// We need a sperator to define each part of the request | |
var boundary = "blob"; | |
// And we'll store our body request as a string. | |
var data = ""; | |
// So, if the user has selected a file | |
if (file.dom.files[0]) { | |
// We start a new part in our body's request | |
data += "--" + boundary + "\r\n"; | |
// We said it's form data (it could be something else) | |
data += 'content-disposition: form-data; ' | |
// We define the name of the form data | |
+ 'name="' + file.dom.name + '"; ' | |
// We provide the real name of the file | |
+ 'filename="' + file.dom.files[0].name + '"\r\n'; | |
// We provide the MIME type of the file | |
data += 'Content-Type: ' + file.dom.files[0].type + '\r\n'; | |
// There is always a blank line between the meta-data and the data | |
data += '\r\n'; | |
// We happen the binary data to our body's request | |
data += file.binary + '\r\n'; | |
} | |
// For text data, it's simpler | |
// We start a new part in our body's request | |
data += "--" + boundary + "\r\n"; | |
// We said it's form data and give it a name | |
data += 'content-disposition: form-data; name="' + text.name + '"\r\n'; | |
// There is always a blank line between the meta-data and the data | |
data += '\r\n'; | |
// We happen the text data to our body's request | |
data += text.value + "\r\n"; | |
// Once we are done, we "close" the body's request | |
data += "--" + boundary + "--"; | |
// We define what will happen if the data are successfully sent | |
XHR.addEventListener('load', function(event) { | |
alert('Yeah! Data sent and response loaded.'); | |
}); | |
// We define what will happen in case of error | |
XHR.addEventListener('error', function(event) { | |
alert('Oups! Something goes wrong.'); | |
}); | |
// We setup our request | |
XHR.open('POST', 'http://ucommbieber.unl.edu/CORS/cors.php'); | |
// We add the required HTTP header to handle a multipart form data POST request | |
XHR.setRequestHeader('Content-Type','multipart/form-data; boundary=' + boundary); | |
XHR.setRequestHeader('Content-Length', data.length); | |
// And finally, We send our data. | |
// Due to Firefox's bug 416178, it's required to use sendAsBinary() instead of send() | |
XHR.sendAsBinary(data); | |
} | |
// At least, We need to access our form | |
var form = document.getElementById("myForm"); | |
// to take over the submit event | |
form.addEventListener('submit', function (event) { | |
event.preventDefault(); | |
sendData(); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Sending_forms_through_JavaScript --> | |
<form id="myForm"> | |
<p> | |
<label for="i1">text data:</label> | |
<input id="i1" name="myText" value="Some text data"> | |
</p> | |
<p> | |
<label for="i2">file data:</label> | |
<input id="i2" name="myFile" type="file"> | |
</p> | |
<button>Send Me!</button> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Sending_forms_through_JavaScript