Created
April 16, 2025 12:07
-
-
Save Deankinyua/875f9315746931116ef20206edbab7c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // * we need a slice of JavaScript that the client will invoke to | |
| // * send a post request to the signed url with the signed form data | |
| // the name S3 matches what we declared in our SimpleS3Upload module | |
| let Uploaders = {}; | |
| Uploaders.S3 = function (entries, onViewError) { | |
| entries.forEach((entry) => { | |
| // Prepares an AJAX request | |
| let xhr = new XMLHttpRequest(); | |
| onViewError(() => xhr.abort()); | |
| xhr.onload = () => | |
| xhr.status === 200 ? entry.progress(100) : entry.error(); | |
| xhr.onerror = () => entry.error(); | |
| xhr.upload.addEventListener("progress", (event) => { | |
| if (event.lengthComputable) { | |
| let percent = Math.round((event.loaded / event.total) * 100); | |
| if (percent < 100) { | |
| entry.progress(percent); | |
| } | |
| } | |
| }); | |
| let url = entry.meta.url; | |
| xhr.open("PUT", url, true); | |
| xhr.send(entry.file); | |
| }); | |
| }; | |
| export default Uploaders; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment