drag.html 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Uppy</title>
  6. <link href="https://releases.transloadit.com/uppy/v3.14.0/uppy.min.css" rel="stylesheet">
  7. </head>
  8. <body>
  9. <section class="example">
  10. <div id="drag-drop-area"></div>
  11. <div class="for-ProgressBar"></div>
  12. <div class="uploaded-files">
  13. <h5>Uploaded files:</h5>
  14. <ol></ol>
  15. </div>
  16. <script type="module">
  17. import {Uppy, DragDrop, ProgressBar, AwsS3 } from "https://releases.transloadit.com/uppy/v3.14.0/uppy.min.mjs"
  18. // Function for displaying uploaded files
  19. const onUploadSuccess = (elForUploadedFiles) => (file, response) => {
  20. const url = response.uploadURL
  21. const fileName = file.name
  22. const li = document.createElement('li')
  23. const a = document.createElement('a')
  24. a.href = url
  25. a.target = '_blank'
  26. a.appendChild(document.createTextNode(fileName))
  27. li.appendChild(a)
  28. document.querySelector(elForUploadedFiles).appendChild(li)
  29. }
  30. var uppy = new Uppy({
  31. autoProceed: true,
  32. restrictions: {
  33. maxNumberOfFiles: 10,
  34. }
  35. })
  36. .use(DragDrop, {
  37. inline: true,
  38. target: '#drag-drop-area'
  39. })
  40. .use(ProgressBar, { target: '.example .for-ProgressBar', hideAfterFinish: true })
  41. .use(AwsS3, {
  42. getUploadParameters (file) {
  43. // Send a request to our PHP signing endpoint.
  44. return fetch('/sign-s3', {
  45. method: 'post',
  46. // Send and receive JSON.
  47. headers: {
  48. accept: 'application/json',
  49. 'content-type': 'application/json',
  50. },
  51. body: JSON.stringify({
  52. filename: file.name,
  53. contentType: file.type,
  54. }),
  55. }).then((response) => {
  56. // Parse the JSON response.
  57. return response.json()
  58. }).then((data) => {
  59. // Return an object in the correct shape.
  60. return {
  61. method: data.method,
  62. url: data.url,
  63. fields: data.fields,
  64. // Provide content type header required by S3
  65. headers: {
  66. 'Content-Type': file.type,
  67. },
  68. }
  69. })
  70. },
  71. });
  72. uppy.on('complete', (result) => {
  73. console.log('Upload complete! We’ve uploaded these files:', result.successful)
  74. });
  75. uppy.on('upload-success', onUploadSuccess('.example .uploaded-files ol'));
  76. </script>
  77. </section>
  78. </body>
  79. </html>