|
5 | 5 | <title>Ajax upload form</title>
|
6 | 6 | </head>
|
7 | 7 | <body>
|
| 8 | + |
| 9 | +<p id="support-notice">Your browser does not support Ajax uploads :-(<br/>The form will be submitted as normal.</p> |
| 10 | + |
8 | 11 | <form action="/" method="post" enctype="multipart/form-data" id="form-id">
|
9 |
| - <p><input id="file-id" type="file" name="our-file" /> <input type="button" value="Upload" id="upload-button-id" /></p> |
| 12 | + <p><input id="file-id" type="file" name="our-file" /> <input type="button" value="Upload" id="upload-button-id" disabled="disabled" /></p> |
10 | 13 | <p><label>Some other field: <input name="other-field" type="text" id="other-field-id" /></label></p>
|
11 |
| - <p><input type="submit" value="Save" /></p> |
| 14 | + <p><input type="submit" value="Submit" /></p> |
12 | 15 | <script>
|
| 16 | +function supportAjaxUploadWithProgress() { |
| 17 | + return supportFileAPI() && supportAjaxUploadProgressEvents() && supportFormData(); |
| 18 | + |
| 19 | + function supportFileAPI() { |
| 20 | + var fi = document.createElement('INPUT'); |
| 21 | + fi.type = 'file'; |
| 22 | + return 'files' in fi; |
| 23 | + }; |
| 24 | + |
| 25 | + function supportAjaxUploadProgressEvents() { |
| 26 | + var xhr = new XMLHttpRequest(); |
| 27 | + return !! (xhr && ('upload' in xhr) && ('onprogress' in xhr.upload)); |
| 28 | + }; |
| 29 | + |
| 30 | + function supportFormData() { |
| 31 | + return !! window.FormData; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +if (supportAjaxUploadWithProgress()) { |
| 36 | + var notice = document.getElementById('support-notice'); |
| 37 | + var uploadBtn = document.getElementById('upload-button-id'); |
| 38 | + notice.innerHTML = "Your browser supports HTML uploads. Go try me! :-)"; |
| 39 | + uploadBtn.removeAttribute('disabled'); |
| 40 | +} |
| 41 | + |
13 | 42 | var form = document.getElementById('form-id');
|
14 | 43 | form.onsubmit = function() {
|
15 | 44 | var formData = new FormData(form);
|
|
0 commit comments