Created
September 10, 2014 06:33
-
-
Save kevinwucodes/da59b5160c33cf0798a1 to your computer and use it in GitHub Desktop.
file upload test using FormData and XMLHttpRequest
This file contains hidden or 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
<html> | |
<head> | |
<title>FormData file test</title> | |
<SCRIPT TYPE="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></SCRIPT> | |
</head> | |
<body> | |
<input id="filebutton" type='file' /> | |
<br/> | |
<button id="uploadbutton">click to upload!</button> | |
<div id="statusCode"></div> | |
<div id="response"></div> | |
<script TYPE="text/javascript"> | |
var url = ""; | |
var tripID = ""; | |
var request = new XMLHttpRequest(); | |
$(document).ready(function() { | |
$("#uploadbutton").click(function() { | |
var payload = new FormData(); | |
payload.append("tripID", tripID); | |
payload.append("imageType", "JPG"); | |
payload.append("type", "hazard"); | |
payload.append("comment", "something here"); | |
payload.append("point", '{"lat": 34.409094, "long": -119.854158, "epoch": 1405659960723}'); | |
payload.append("blob", document.getElementById('filebutton').files[0]) | |
request.onreadystatechange = function(XMLHttpRequestProgressEvent) { | |
// we only care when it's done (readyState == 4) | |
if (XMLHttpRequestProgressEvent.target.readyState == 4) { | |
$('#statusCode').text(XMLHttpRequestProgressEvent.target.status); | |
$('#response').text(XMLHttpRequestProgressEvent.target.responseText); | |
} | |
} | |
request.open("POST", url); | |
request.send(payload); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment