drag.html 3.0 KB

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