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