| 1 | require 'sinatra' |
| 2 | require "sinatra/reloader" |
| 3 | |
| 4 | get '/htmx' do |
| 5 | content_type 'text/javascript' |
| 6 | file = File.open("../../src/htmx.js") |
| 7 | return file.read |
| 8 | end |
| 9 | |
| 10 | get '/' do |
| 11 | "<html> |
| 12 | <head> |
| 13 | <script src='htmx'></script> |
| 14 | <script src='https://unpkg.com/[email protected]/dist/_hyperscript.js'></script> |
| 15 | </head> |
| 16 | <body> |
| 17 | <h1>File Upload Example2</h1> |
| 18 | <h2>Plain Javascript</h2> |
| 19 | <form id='form1' hx-encoding='multipart/form-data' hx-post='/'> |
| 20 | <input id='file' type='file' name='file'> |
| 21 | <button > |
| 22 | Upload |
| 23 | </button> |
| 24 | <progress id='progress1' value='0' max='100'></progress> |
| 25 | </form> |
| 26 | <script> |
| 27 | htmx.on('#form1', 'htmx:xhr:progress', function(evt) { |
| 28 | htmx.find('#progress1').setAttribute('value', evt.detail.loaded/evt.detail.total * 100) |
| 29 | }); |
| 30 | </script> |
| 31 | <h2>Hyperscript</h2> |
| 32 | <form hx-encoding='multipart/form-data' hx-post='/' |
| 33 | _='on htmx:xhr:progress(loaded, total) set #progress2.value to (loaded/total)*100'> |
| 34 | <input id='file2' type='file' name='file'> |
| 35 | <button > |
| 36 | Upload |
| 37 | </button> |
| 38 | <progress id='progress2' value='0' max='100'></progress> |
| 39 | </form> |
| 40 | |
| 41 | </body> |
| 42 | </html>" |
| 43 | end |
| 44 | |
| 45 | post '/' do |
| 46 | tempfile = params['file'][:tempfile] |
| 47 | return "File Uploaded to #{tempfile.path}..." |
| 48 | end |