Created
April 23, 2018 08:18
-
-
Save Celestz/34c7886541bd971bc3e45112218a00e2 to your computer and use it in GitHub Desktop.
Using jQuery Ajax with https://file.io
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
$(<file element name>).on('change',function(){ | |
var filename = $('#uploadname').val() | |
var data = new FormData() | |
var thisElement = $(this) | |
$.each(thisElement[0].files, function(i, file) { | |
data.append('file', file) | |
}) | |
$('.loadingOverlay').show() //TOTALLY OPTIONAL, Just an overlay element I call for the form. | |
$.ajax({ | |
xhr: function(){ //TOTALLY OPTIONAL, I just needed this for the progress bar display, you can use it or not. | |
var xhr = new window.XMLHttpRequest() | |
//Upload progress | |
xhr.upload.addEventListener("progress", function(evt){ | |
if (evt.lengthComputable) { | |
var percentComplete = evt.loaded / evt.total | |
//Do something with upload progress | |
$('#fileProgress').val(percentComplete) | |
} | |
}, false) | |
//Download progress | |
xhr.addEventListener("progress", function(evt){ | |
if (evt.lengthComputable) { | |
var percentComplete = evt.loaded / evt.total | |
//Do something with download progress | |
} | |
}, false) | |
return xhr | |
}, | |
url: "https://file.io?expires=1m", | |
data: data, | |
cache: false, | |
contentType: false, | |
processData: false, | |
method: 'POST', | |
type: 'POST', // For jQuery < 1.9 | |
success: function (d) { | |
//Do what you want with the return data | |
} | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment