|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <title>Ajax upload form</title> |
| 6 | +</head> |
| 7 | +<body> |
| 8 | + |
| 9 | +<!-- |
| 10 | + By default, we assume Ajax uploads are not supported. |
| 11 | + Later we'll detect support and change this message if found. |
| 12 | +--> |
| 13 | +<p id="support-notice">Your browser does not support Ajax uploads :-(<br/>The form will be submitted as normal.</p> |
| 14 | + |
| 15 | +<!-- The form starts --> |
| 16 | +<form action="/" method="post" enctype="multipart/form-data" id="form-id"> |
| 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 --> |
| 28 | + <p><label>Some other field: <input name="other-field" type="text" id="other-field-id" /></label></p> |
| 29 | + |
| 30 | + <!-- And finally a submit button --> |
| 31 | + <p><input type="submit" value="Submit" /></p> |
| 32 | + |
| 33 | + <script> |
| 34 | +// Function that will allow us to know if Ajax uploads are supported |
| 35 | +function supportAjaxUploadWithProgress() { |
| 36 | + return supportFileAPI() && supportAjaxUploadProgressEvents() && supportFormData(); |
| 37 | + |
| 38 | + // Is the File API supported? |
| 39 | + function supportFileAPI() { |
| 40 | + var fi = document.createElement('INPUT'); |
| 41 | + fi.type = 'file'; |
| 42 | + return 'files' in fi; |
| 43 | + }; |
| 44 | + |
| 45 | + // Are progress events supported? |
| 46 | + function supportAjaxUploadProgressEvents() { |
| 47 | + var xhr = new XMLHttpRequest(); |
| 48 | + return !! (xhr && ('upload' in xhr) && ('onprogress' in xhr.upload)); |
| 49 | + }; |
| 50 | + |
| 51 | + // Is FormData supported? |
| 52 | + function supportFormData() { |
| 53 | + return !! window.FormData; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Actually confirm support |
| 58 | +if (supportAjaxUploadWithProgress()) { |
| 59 | + // Ajax uploads are supported! |
| 60 | + // Change the support message and enable the upload button |
| 61 | + var notice = document.getElementById('support-notice'); |
| 62 | + var uploadBtn = document.getElementById('upload-button-id'); |
| 63 | + notice.innerHTML = "Your browser supports HTML uploads. Go try me! :-)"; |
| 64 | + uploadBtn.removeAttribute('disabled'); |
| 65 | + |
| 66 | + // Init the Ajax form submission |
| 67 | + initFullFormAjaxUpload(); |
| 68 | + |
| 69 | + // Init the single-field file upload |
| 70 | + initFileOnlyAjaxUpload(); |
| 71 | +} |
| 72 | + |
| 73 | +function initFullFormAjaxUpload() { |
| 74 | + var form = document.getElementById('form-id'); |
| 75 | + form.onsubmit = function() { |
| 76 | + // FormData receives the whole form |
| 77 | + var formData = new FormData(form); |
| 78 | + |
| 79 | + // We send the data where the form wanted |
| 80 | + var action = form.getAttribute('action'); |
| 81 | + |
| 82 | + // Code common to both variants |
| 83 | + sendXHRequest(formData, action); |
| 84 | + |
| 85 | + // Avoid normal form submission |
| 86 | + return false; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +function initFileOnlyAjaxUpload() { |
| 91 | + var uploadBtn = document.getElementById('upload-button-id'); |
| 92 | + uploadBtn.onclick = function (evt) { |
| 93 | + var formData = new FormData(); |
| 94 | + |
| 95 | + // Since this is the file only, we send it to a specific location |
| 96 | + var action = '/upload'; |
| 97 | + |
| 98 | + // FormData only has the file |
| 99 | + var fileInput = document.getElementById('file-id'); |
| 100 | + var file = fileInput.files[0]; |
| 101 | + formData.append('our-file', file); |
| 102 | + |
| 103 | + // Code common to both variants |
| 104 | + sendXHRequest(formData, action); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 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 |
| 111 | +function sendXHRequest(formData, uri) { |
| 112 | + // Get an XMLHttpRequest instance |
| 113 | + var xhr = new XMLHttpRequest(); |
| 114 | + |
| 115 | + // Set up events |
| 116 | + xhr.upload.addEventListener('loadstart', onloadstartHandler, false); |
| 117 | + xhr.upload.addEventListener('progress', onprogressHandler, false); |
| 118 | + xhr.upload.addEventListener('load', onloadHandler, false); |
| 119 | + xhr.addEventListener('readystatechange', onreadystatechangeHandler, false); |
| 120 | + |
| 121 | + // Set up request |
| 122 | + xhr.open('POST', uri, true); |
| 123 | + |
| 124 | + // Fire! |
| 125 | + xhr.send(formData); |
| 126 | +} |
| 127 | + |
| 128 | +// Handle the start of the transmission |
| 129 | +function onloadstartHandler(evt) { |
| 130 | + var div = document.getElementById('upload-status'); |
| 131 | + div.innerHTML = 'Upload started!'; |
| 132 | +} |
| 133 | + |
| 134 | +// Handle the end of the transmission |
| 135 | +function onloadHandler(evt) { |
| 136 | + var div = document.getElementById('upload-status'); |
| 137 | + div.innerHTML = 'Upload successful!'; |
| 138 | +} |
| 139 | + |
| 140 | +// Handle the progress |
| 141 | +function onprogressHandler(evt) { |
| 142 | + var div = document.getElementById('progress'); |
| 143 | + var percent = evt.loaded/evt.total*100; |
| 144 | + div.innerHTML = 'Progress: ' + percent + '%'; |
| 145 | +} |
| 146 | + |
| 147 | +// Handle the response from the server |
| 148 | +function onreadystatechangeHandler(evt) { |
| 149 | + var status = null; |
| 150 | + |
| 151 | + try { |
| 152 | + status = evt.target.status; |
| 153 | + } |
| 154 | + catch(e) { |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + if (status == '200' && evt.target.responseText) { |
| 159 | + var result = document.getElementById('result'); |
| 160 | + result.innerHTML = '<p>The server saw it as:</p><pre>' + evt.target.responseText + '</pre>'; |
| 161 | + } |
| 162 | +} |
| 163 | + </script> |
| 164 | + |
| 165 | + <!-- Placeholders for messages set by event handlers --> |
| 166 | + <p id="upload-status"></p> |
| 167 | + <p id="progress"></p> |
| 168 | + <pre id="result"></pre> |
| 169 | + |
| 170 | +</form> |
| 171 | +</body> |
| 172 | +</html> |
0 commit comments