Fork of Smoothie to port to mbed non-LPC targets.
Fork of Smoothie by
libs/Network/uip/webserver/httpd-fs/index.html@2:1df0b61d3b5a, 2014-02-28 (annotated)
- Committer:
- Michael J. Spencer
- Date:
- Fri Feb 28 18:52:52 2014 -0800
- Revision:
- 2:1df0b61d3b5a
Update to latest Smoothie.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Michael J. Spencer |
2:1df0b61d3b5a | 1 | <!doctype html> |
Michael J. Spencer |
2:1df0b61d3b5a | 2 | <head> |
Michael J. Spencer |
2:1df0b61d3b5a | 3 | <meta charset="utf-8"> |
Michael J. Spencer |
2:1df0b61d3b5a | 4 | <title>Single Command</title> |
Michael J. Spencer |
2:1df0b61d3b5a | 5 | <script src="http://code.jquery.com/jquery-1.9.1.js"></script> |
Michael J. Spencer |
2:1df0b61d3b5a | 6 | <script type=text/javascript language=JavaScript> |
Michael J. Spencer |
2:1df0b61d3b5a | 7 | var concentric_circle_radii = [11, 45, 69, 94, 115]; |
Michael J. Spencer |
2:1df0b61d3b5a | 8 | var center = [124, 121]; |
Michael J. Spencer |
2:1df0b61d3b5a | 9 | var spacer = 7; |
Michael J. Spencer |
2:1df0b61d3b5a | 10 | var zbutton_ydistances = [7, 30, 55, 83]; |
Michael J. Spencer |
2:1df0b61d3b5a | 11 | var zcenter = [30, 118]; |
Michael J. Spencer |
2:1df0b61d3b5a | 12 | |
Michael J. Spencer |
2:1df0b61d3b5a | 13 | function runCommand(cmd) { |
Michael J. Spencer |
2:1df0b61d3b5a | 14 | // Get some values from elements on the page: |
Michael J. Spencer |
2:1df0b61d3b5a | 15 | var $form = $( "#commandForm" ); |
Michael J. Spencer |
2:1df0b61d3b5a | 16 | cmd += "\n"; |
Michael J. Spencer |
2:1df0b61d3b5a | 17 | url = "/command_silent"; // $form.attr( "action" ); |
Michael J. Spencer |
2:1df0b61d3b5a | 18 | // Send the data using post |
Michael J. Spencer |
2:1df0b61d3b5a | 19 | var posting = $.post( url, cmd ); |
Michael J. Spencer |
2:1df0b61d3b5a | 20 | // Put the results in a div |
Michael J. Spencer |
2:1df0b61d3b5a | 21 | // posting.done(function( data ) { |
Michael J. Spencer |
2:1df0b61d3b5a | 22 | // $( "#result" ).empty(); |
Michael J. Spencer |
2:1df0b61d3b5a | 23 | // $.each(data.split('\n'), function(index) { |
Michael J. Spencer |
2:1df0b61d3b5a | 24 | // $( "#result" ).append( this + '<br/>' ); |
Michael J. Spencer |
2:1df0b61d3b5a | 25 | // }); |
Michael J. Spencer |
2:1df0b61d3b5a | 26 | // }); |
Michael J. Spencer |
2:1df0b61d3b5a | 27 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 28 | |
Michael J. Spencer |
2:1df0b61d3b5a | 29 | function lookupConcentric(radius){ |
Michael J. Spencer |
2:1df0b61d3b5a | 30 | var length = concentric_circle_radii.length; |
Michael J. Spencer |
2:1df0b61d3b5a | 31 | for (i=0;i<=length;i++) { |
Michael J. Spencer |
2:1df0b61d3b5a | 32 | if (radius < concentric_circle_radii[i]) return(i); |
Michael J. Spencer |
2:1df0b61d3b5a | 33 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 34 | return(length); |
Michael J. Spencer |
2:1df0b61d3b5a | 35 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 36 | |
Michael J. Spencer |
2:1df0b61d3b5a | 37 | function getQuadrantConcentricFromPosition(x,y) { |
Michael J. Spencer |
2:1df0b61d3b5a | 38 | var rel_x = x - center[0] |
Michael J. Spencer |
2:1df0b61d3b5a | 39 | var rel_y = y - center[1] |
Michael J. Spencer |
2:1df0b61d3b5a | 40 | var radius = Math.sqrt(Math.pow(Math.abs(rel_x),2) + Math.pow(Math.abs(rel_y),2)) |
Michael J. Spencer |
2:1df0b61d3b5a | 41 | if (rel_x > rel_y && rel_x > -rel_y) { |
Michael J. Spencer |
2:1df0b61d3b5a | 42 | quadrant = 0; // Right |
Michael J. Spencer |
2:1df0b61d3b5a | 43 | } else if (rel_x <= rel_y && rel_x > -rel_y) { |
Michael J. Spencer |
2:1df0b61d3b5a | 44 | quadrant = 3; // Down |
Michael J. Spencer |
2:1df0b61d3b5a | 45 | } else if (rel_x > rel_y && rel_x < -rel_y) { |
Michael J. Spencer |
2:1df0b61d3b5a | 46 | quadrant = 1; // Up |
Michael J. Spencer |
2:1df0b61d3b5a | 47 | } else { |
Michael J. Spencer |
2:1df0b61d3b5a | 48 | quadrant = 2; // Left |
Michael J. Spencer |
2:1df0b61d3b5a | 49 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 50 | var idx = lookupConcentric(radius); |
Michael J. Spencer |
2:1df0b61d3b5a | 51 | return [quadrant, idx] |
Michael J. Spencer |
2:1df0b61d3b5a | 52 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 53 | |
Michael J. Spencer |
2:1df0b61d3b5a | 54 | function clickXY(event){ |
Michael J. Spencer |
2:1df0b61d3b5a | 55 | var pos_x = event.offsetX?(event.offsetX):event.pageX-document.getElementById("control_xy").offsetLeft; |
Michael J. Spencer |
2:1df0b61d3b5a | 56 | var pos_y = event.offsetY?(event.offsetY):event.pageY-document.getElementById("control_xy").offsetTop; |
Michael J. Spencer |
2:1df0b61d3b5a | 57 | var codes = getQuadrantConcentricFromPosition(pos_x,pos_y); |
Michael J. Spencer |
2:1df0b61d3b5a | 58 | var quadrant = codes[0], concentric = codes[1]; |
Michael J. Spencer |
2:1df0b61d3b5a | 59 | if (concentric < 5) { // movement button pressed |
Michael J. Spencer |
2:1df0b61d3b5a | 60 | var xdir = [1, 0, -1, 0, 0, 0][quadrant]; |
Michael J. Spencer |
2:1df0b61d3b5a | 61 | var ydir = [0, 1, 0, -1, 0, 0][quadrant]; |
Michael J. Spencer |
2:1df0b61d3b5a | 62 | var magnitude = Math.pow(10, concentric - 2); |
Michael J. Spencer |
2:1df0b61d3b5a | 63 | if (xdir != 0) { |
Michael J. Spencer |
2:1df0b61d3b5a | 64 | command = "G1 X" + (magnitude * xdir) + " F" + document.getElementById("xy_velocity").value; |
Michael J. Spencer |
2:1df0b61d3b5a | 65 | } else { |
Michael J. Spencer |
2:1df0b61d3b5a | 66 | command = "G1 Y" + (magnitude * ydir) + " F" + document.getElementById("xy_velocity").value; |
Michael J. Spencer |
2:1df0b61d3b5a | 67 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 68 | runCommand("G91 " + command + " G90"); |
Michael J. Spencer |
2:1df0b61d3b5a | 69 | } else { // home button pressed |
Michael J. Spencer |
2:1df0b61d3b5a | 70 | if (pos_x < 49 && pos_y < 49) { // home x button |
Michael J. Spencer |
2:1df0b61d3b5a | 71 | command = "G28 X0"; |
Michael J. Spencer |
2:1df0b61d3b5a | 72 | } else if (pos_x > 200 && pos_y < 49) { //home y button |
Michael J. Spencer |
2:1df0b61d3b5a | 73 | command = "G28 Y0"; |
Michael J. Spencer |
2:1df0b61d3b5a | 74 | } else if (pos_x < 49 && pos_y > 200) { // home all button |
Michael J. Spencer |
2:1df0b61d3b5a | 75 | command = "G28"; |
Michael J. Spencer |
2:1df0b61d3b5a | 76 | } else { // home z button |
Michael J. Spencer |
2:1df0b61d3b5a | 77 | command = "G28 Z0"; |
Michael J. Spencer |
2:1df0b61d3b5a | 78 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 79 | runCommand(command); |
Michael J. Spencer |
2:1df0b61d3b5a | 80 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 81 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 82 | |
Michael J. Spencer |
2:1df0b61d3b5a | 83 | function lookupRange(ydist) { |
Michael J. Spencer |
2:1df0b61d3b5a | 84 | var length = zbutton_ydistances.length; |
Michael J. Spencer |
2:1df0b61d3b5a | 85 | for (i=0;i<length;i++) { |
Michael J. Spencer |
2:1df0b61d3b5a | 86 | if (ydist < zbutton_ydistances[i]) return i; |
Michael J. Spencer |
2:1df0b61d3b5a | 87 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 88 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 89 | |
Michael J. Spencer |
2:1df0b61d3b5a | 90 | function clickZ(event){ |
Michael J. Spencer |
2:1df0b61d3b5a | 91 | //var pos_x = event.offsetX?(event.offsetX):event.pageX-document.getElementById("control_z").offsetLeft; |
Michael J. Spencer |
2:1df0b61d3b5a | 92 | var pos_y = event.offsetY?(event.offsetY):event.pageY-document.getElementById("control_z").offsetTop; |
Michael J. Spencer |
2:1df0b61d3b5a | 93 | var ydelta = zcenter[1] - pos_y; |
Michael J. Spencer |
2:1df0b61d3b5a | 94 | var range = lookupRange(Math.abs(ydelta)); |
Michael J. Spencer |
2:1df0b61d3b5a | 95 | var direction = (ydelta > 0)?1:-1; |
Michael J. Spencer |
2:1df0b61d3b5a | 96 | if (range < 4) { |
Michael J. Spencer |
2:1df0b61d3b5a | 97 | runCommand("G91 G1 Z" + (Math.pow(10,range-2) * direction) + " F" + document.getElementById("z_velocity").value + " G90"); |
Michael J. Spencer |
2:1df0b61d3b5a | 98 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 99 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 100 | |
Michael J. Spencer |
2:1df0b61d3b5a | 101 | function extrude(event,a,b) { |
Michael J. Spencer |
2:1df0b61d3b5a | 102 | var length = document.getElementById("extrude_length").value; |
Michael J. Spencer |
2:1df0b61d3b5a | 103 | var velocity = document.getElementById("extrude_velocity").value; |
Michael J. Spencer |
2:1df0b61d3b5a | 104 | var direction = (event.currentTarget.id=='extrude')?1:-1; |
Michael J. Spencer |
2:1df0b61d3b5a | 105 | runCommand("G91 G1 E" + (length * direction) + " F" + velocity + " G90"); |
Michael J. Spencer |
2:1df0b61d3b5a | 106 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 107 | |
Michael J. Spencer |
2:1df0b61d3b5a | 108 | function motorsOff(event) { |
Michael J. Spencer |
2:1df0b61d3b5a | 109 | runCommand("M18"); |
Michael J. Spencer |
2:1df0b61d3b5a | 110 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 111 | |
Michael J. Spencer |
2:1df0b61d3b5a | 112 | function heatSet(event) { |
Michael J. Spencer |
2:1df0b61d3b5a | 113 | var type = (event.currentTarget.id=='heat_set')?104:140; |
Michael J. Spencer |
2:1df0b61d3b5a | 114 | var temperature = (type==104)?document.getElementById("heat_value").value:document.getElementById("bed_value").value; |
Michael J. Spencer |
2:1df0b61d3b5a | 115 | runCommand("M" + type + " S" + temperature); |
Michael J. Spencer |
2:1df0b61d3b5a | 116 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 117 | |
Michael J. Spencer |
2:1df0b61d3b5a | 118 | function heatOff(event) { |
Michael J. Spencer |
2:1df0b61d3b5a | 119 | var type = (event.currentTarget.id=='heat_off')?104:140; |
Michael J. Spencer |
2:1df0b61d3b5a | 120 | runCommand("M" + type + " S0"); |
Michael J. Spencer |
2:1df0b61d3b5a | 121 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 122 | |
Michael J. Spencer |
2:1df0b61d3b5a | 123 | </script> |
Michael J. Spencer |
2:1df0b61d3b5a | 124 | </head> |
Michael J. Spencer |
2:1df0b61d3b5a | 125 | |
Michael J. Spencer |
2:1df0b61d3b5a | 126 | <body> |
Michael J. Spencer |
2:1df0b61d3b5a | 127 | <h1>Welcome to Smoothie web interface</h1> |
Michael J. Spencer |
2:1df0b61d3b5a | 128 | |
Michael J. Spencer |
2:1df0b61d3b5a | 129 | <button id=motors_off onclick=motorsOff(event)>Motors Off</button> |
Michael J. Spencer |
2:1df0b61d3b5a | 130 | XY:<input type=text id=xy_velocity size=4 value=3000 style=width:50px>mm/min |
Michael J. Spencer |
2:1df0b61d3b5a | 131 | Z:<input type=text id=z_velocity size=3 value=200 style=width:40px> |
Michael J. Spencer |
2:1df0b61d3b5a | 132 | <br> |
Michael J. Spencer |
2:1df0b61d3b5a | 133 | <img id=control_xy src=img/control_xy.png onclick=clickXY(event)> |
Michael J. Spencer |
2:1df0b61d3b5a | 134 | <img id=control_z src=img/control_z.png onclick=clickZ(event)> |
Michael J. Spencer |
2:1df0b61d3b5a | 135 | <br> |
Michael J. Spencer |
2:1df0b61d3b5a | 136 | <table><tr><td> |
Michael J. Spencer |
2:1df0b61d3b5a | 137 | <table> |
Michael J. Spencer |
2:1df0b61d3b5a | 138 | <tr> |
Michael J. Spencer |
2:1df0b61d3b5a | 139 | <td style=text-align:right>Heat:</td> |
Michael J. Spencer |
2:1df0b61d3b5a | 140 | <td><button id=heat_off onclick=heatOff(event)>Off</button></td> |
Michael J. Spencer |
2:1df0b61d3b5a | 141 | <td><input type=text id=heat_value size=3 style=width:40px value=0></td> |
Michael J. Spencer |
2:1df0b61d3b5a | 142 | <td><button id=heat_set onclick=heatSet(event)>Set</button></td> |
Michael J. Spencer |
2:1df0b61d3b5a | 143 | </tr> |
Michael J. Spencer |
2:1df0b61d3b5a | 144 | <tr> |
Michael J. Spencer |
2:1df0b61d3b5a | 145 | <td style=text-align:right>Bed:</td> |
Michael J. Spencer |
2:1df0b61d3b5a | 146 | <td><button id=bed_off onclick=heatOff(event)>Off</button></td> |
Michael J. Spencer |
2:1df0b61d3b5a | 147 | <td><input type=text id=bed_value size=3 style=width:40px value=0></td> |
Michael J. Spencer |
2:1df0b61d3b5a | 148 | <td><button id=bed_set onclick=heatSet(event)>Set</button></td> |
Michael J. Spencer |
2:1df0b61d3b5a | 149 | </tr> |
Michael J. Spencer |
2:1df0b61d3b5a | 150 | </table> |
Michael J. Spencer |
2:1df0b61d3b5a | 151 | </td><td valign=top> |
Michael J. Spencer |
2:1df0b61d3b5a | 152 | <button id=get_temperature onclick=runCommand("M105")>Get Temperature</button> |
Michael J. Spencer |
2:1df0b61d3b5a | 153 | </td></tr></table> |
Michael J. Spencer |
2:1df0b61d3b5a | 154 | <br> |
Michael J. Spencer |
2:1df0b61d3b5a | 155 | <button id=extrude onclick=extrude(event)>Extrude</button> |
Michael J. Spencer |
2:1df0b61d3b5a | 156 | <button id=reverse onclick=extrude(event)>Reverse</button><br> |
Michael J. Spencer |
2:1df0b61d3b5a | 157 | <input type=text id=extrude_length value=5 size=3 style=width:35px> |
Michael J. Spencer |
2:1df0b61d3b5a | 158 | mm @ |
Michael J. Spencer |
2:1df0b61d3b5a | 159 | <input type=text id=extrude_velocity value=100 size=3 style=width:40px> |
Michael J. Spencer |
2:1df0b61d3b5a | 160 | mm/min |
Michael J. Spencer |
2:1df0b61d3b5a | 161 | |
Michael J. Spencer |
2:1df0b61d3b5a | 162 | <h2>Commands</h2> |
Michael J. Spencer |
2:1df0b61d3b5a | 163 | <form action="/command" id="commandForm"> |
Michael J. Spencer |
2:1df0b61d3b5a | 164 | <input type="text" name="commandText" placeholder="Send Command..."> |
Michael J. Spencer |
2:1df0b61d3b5a | 165 | <input type="submit" value="Send"> |
Michael J. Spencer |
2:1df0b61d3b5a | 166 | </form> |
Michael J. Spencer |
2:1df0b61d3b5a | 167 | <!-- the result of the command will be rendered inside this div --> |
Michael J. Spencer |
2:1df0b61d3b5a | 168 | <div id="result"></div> |
Michael J. Spencer |
2:1df0b61d3b5a | 169 | <script> |
Michael J. Spencer |
2:1df0b61d3b5a | 170 | // Attach a submit handler to the form |
Michael J. Spencer |
2:1df0b61d3b5a | 171 | $( "#commandForm" ).submit(function( event ) { |
Michael J. Spencer |
2:1df0b61d3b5a | 172 | // Stop form from submitting normally |
Michael J. Spencer |
2:1df0b61d3b5a | 173 | event.preventDefault(); |
Michael J. Spencer |
2:1df0b61d3b5a | 174 | // Get some values from elements on the page: |
Michael J. Spencer |
2:1df0b61d3b5a | 175 | var $form = $( this ); |
Michael J. Spencer |
2:1df0b61d3b5a | 176 | command = $form.find( "input[name='commandText']" ).val(); |
Michael J. Spencer |
2:1df0b61d3b5a | 177 | command += "\n"; |
Michael J. Spencer |
2:1df0b61d3b5a | 178 | url = $form.attr( "action" ); |
Michael J. Spencer |
2:1df0b61d3b5a | 179 | // Send the data using post |
Michael J. Spencer |
2:1df0b61d3b5a | 180 | var posting = $.post( url, command ); |
Michael J. Spencer |
2:1df0b61d3b5a | 181 | // Put the results in a div |
Michael J. Spencer |
2:1df0b61d3b5a | 182 | posting.done(function( data ) { |
Michael J. Spencer |
2:1df0b61d3b5a | 183 | $( "#result" ).empty(); |
Michael J. Spencer |
2:1df0b61d3b5a | 184 | $.each(data.split('\n'), function(index) { |
Michael J. Spencer |
2:1df0b61d3b5a | 185 | $( "#result" ).append( this + '<br/>' ); |
Michael J. Spencer |
2:1df0b61d3b5a | 186 | }); |
Michael J. Spencer |
2:1df0b61d3b5a | 187 | }); |
Michael J. Spencer |
2:1df0b61d3b5a | 188 | }); |
Michael J. Spencer |
2:1df0b61d3b5a | 189 | </script> |
Michael J. Spencer |
2:1df0b61d3b5a | 190 | |
Michael J. Spencer |
2:1df0b61d3b5a | 191 | <h2> Upload File </h2> |
Michael J. Spencer |
2:1df0b61d3b5a | 192 | <input type="file" id="files" name="files[]" onchange="upload();" /> |
Michael J. Spencer |
2:1df0b61d3b5a | 193 | |
Michael J. Spencer |
2:1df0b61d3b5a | 194 | <h3>Uploading file(s)</h3> |
Michael J. Spencer |
2:1df0b61d3b5a | 195 | <output id="list"></output> |
Michael J. Spencer |
2:1df0b61d3b5a | 196 | <div id="progress"></div> |
Michael J. Spencer |
2:1df0b61d3b5a | 197 | <div id="uploadresult"></div> |
Michael J. Spencer |
2:1df0b61d3b5a | 198 | <script> |
Michael J. Spencer |
2:1df0b61d3b5a | 199 | function handleFileSelect(evt) { |
Michael J. Spencer |
2:1df0b61d3b5a | 200 | var files = evt.target.files; // handleFileSelectist object |
Michael J. Spencer |
2:1df0b61d3b5a | 201 | |
Michael J. Spencer |
2:1df0b61d3b5a | 202 | // files is a FileList of File objects. List some properties. |
Michael J. Spencer |
2:1df0b61d3b5a | 203 | var output = []; |
Michael J. Spencer |
2:1df0b61d3b5a | 204 | for (var i = 0, f; f = files[i]; i++) { |
Michael J. Spencer |
2:1df0b61d3b5a | 205 | output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ', |
Michael J. Spencer |
2:1df0b61d3b5a | 206 | f.size, ' bytes, last modified: ', |
Michael J. Spencer |
2:1df0b61d3b5a | 207 | f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a', |
Michael J. Spencer |
2:1df0b61d3b5a | 208 | '</li>'); |
Michael J. Spencer |
2:1df0b61d3b5a | 209 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 210 | document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>'; |
Michael J. Spencer |
2:1df0b61d3b5a | 211 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 212 | |
Michael J. Spencer |
2:1df0b61d3b5a | 213 | document.getElementById('files').addEventListener('change', handleFileSelect, false); |
Michael J. Spencer |
2:1df0b61d3b5a | 214 | |
Michael J. Spencer |
2:1df0b61d3b5a | 215 | |
Michael J. Spencer |
2:1df0b61d3b5a | 216 | function upload() { |
Michael J. Spencer |
2:1df0b61d3b5a | 217 | // take the file from the input |
Michael J. Spencer |
2:1df0b61d3b5a | 218 | var file = document.getElementById('files').files[0]; |
Michael J. Spencer |
2:1df0b61d3b5a | 219 | var reader = new FileReader(); |
Michael J. Spencer |
2:1df0b61d3b5a | 220 | reader.readAsBinaryString(file); // alternatively you can use readAsDataURL |
Michael J. Spencer |
2:1df0b61d3b5a | 221 | reader.onloadend = function(evt) |
Michael J. Spencer |
2:1df0b61d3b5a | 222 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 223 | // create XHR instance |
Michael J. Spencer |
2:1df0b61d3b5a | 224 | xhr = new XMLHttpRequest(); |
Michael J. Spencer |
2:1df0b61d3b5a | 225 | |
Michael J. Spencer |
2:1df0b61d3b5a | 226 | // send the file through POST |
Michael J. Spencer |
2:1df0b61d3b5a | 227 | xhr.open("POST", 'upload', true); |
Michael J. Spencer |
2:1df0b61d3b5a | 228 | xhr.setRequestHeader('X-Filename', file.name); |
Michael J. Spencer |
2:1df0b61d3b5a | 229 | |
Michael J. Spencer |
2:1df0b61d3b5a | 230 | // make sure we have the sendAsBinary method on all browsers |
Michael J. Spencer |
2:1df0b61d3b5a | 231 | XMLHttpRequest.prototype.mySendAsBinary = function(text){ |
Michael J. Spencer |
2:1df0b61d3b5a | 232 | var data = new ArrayBuffer(text.length); |
Michael J. Spencer |
2:1df0b61d3b5a | 233 | var ui8a = new Uint8Array(data, 0); |
Michael J. Spencer |
2:1df0b61d3b5a | 234 | for (var i = 0; i < text.length; i++) ui8a[i] = (text.charCodeAt(i) & 0xff); |
Michael J. Spencer |
2:1df0b61d3b5a | 235 | |
Michael J. Spencer |
2:1df0b61d3b5a | 236 | if(typeof window.Blob == "function") |
Michael J. Spencer |
2:1df0b61d3b5a | 237 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 238 | var blob = new Blob([data]); |
Michael J. Spencer |
2:1df0b61d3b5a | 239 | }else{ |
Michael J. Spencer |
2:1df0b61d3b5a | 240 | var bb = new (window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder)(); |
Michael J. Spencer |
2:1df0b61d3b5a | 241 | bb.append(data); |
Michael J. Spencer |
2:1df0b61d3b5a | 242 | var blob = bb.getBlob(); |
Michael J. Spencer |
2:1df0b61d3b5a | 243 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 244 | |
Michael J. Spencer |
2:1df0b61d3b5a | 245 | this.send(blob); |
Michael J. Spencer |
2:1df0b61d3b5a | 246 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 247 | |
Michael J. Spencer |
2:1df0b61d3b5a | 248 | // let's track upload progress |
Michael J. Spencer |
2:1df0b61d3b5a | 249 | var eventSource = xhr.upload || xhr; |
Michael J. Spencer |
2:1df0b61d3b5a | 250 | eventSource.addEventListener("progress", function(e) { |
Michael J. Spencer |
2:1df0b61d3b5a | 251 | // get percentage of how much of the current file has been sent |
Michael J. Spencer |
2:1df0b61d3b5a | 252 | var position = e.position || e.loaded; |
Michael J. Spencer |
2:1df0b61d3b5a | 253 | var total = e.totalSize || e.total; |
Michael J. Spencer |
2:1df0b61d3b5a | 254 | var percentage = Math.round((position/total)*100); |
Michael J. Spencer |
2:1df0b61d3b5a | 255 | |
Michael J. Spencer |
2:1df0b61d3b5a | 256 | // here you should write your own code how you wish to proces this |
Michael J. Spencer |
2:1df0b61d3b5a | 257 | $( "#progress" ).empty().append('uploaded ' + percentage + '%'); |
Michael J. Spencer |
2:1df0b61d3b5a | 258 | }); |
Michael J. Spencer |
2:1df0b61d3b5a | 259 | |
Michael J. Spencer |
2:1df0b61d3b5a | 260 | // state change observer - we need to know when and if the file was successfully uploaded |
Michael J. Spencer |
2:1df0b61d3b5a | 261 | xhr.onreadystatechange = function() |
Michael J. Spencer |
2:1df0b61d3b5a | 262 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 263 | if(xhr.readyState == 4) |
Michael J. Spencer |
2:1df0b61d3b5a | 264 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 265 | if(xhr.status == 200) |
Michael J. Spencer |
2:1df0b61d3b5a | 266 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 267 | // process success |
Michael J. Spencer |
2:1df0b61d3b5a | 268 | $( "#uploadresult" ).empty().append( 'Uploaded Ok'); |
Michael J. Spencer |
2:1df0b61d3b5a | 269 | }else{ |
Michael J. Spencer |
2:1df0b61d3b5a | 270 | // process error |
Michael J. Spencer |
2:1df0b61d3b5a | 271 | $( "#uploadresult" ).empty().append( 'Uploaded Failed'); |
Michael J. Spencer |
2:1df0b61d3b5a | 272 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 273 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 274 | }; |
Michael J. Spencer |
2:1df0b61d3b5a | 275 | |
Michael J. Spencer |
2:1df0b61d3b5a | 276 | // start sending |
Michael J. Spencer |
2:1df0b61d3b5a | 277 | xhr.mySendAsBinary(evt.target.result); |
Michael J. Spencer |
2:1df0b61d3b5a | 278 | }; |
Michael J. Spencer |
2:1df0b61d3b5a | 279 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 280 | </script> |
Michael J. Spencer |
2:1df0b61d3b5a | 281 | |
Michael J. Spencer |
2:1df0b61d3b5a | 282 | </body> |
Michael J. Spencer |
2:1df0b61d3b5a | 283 | </html> |