Skip to content

Instantly share code, notes, and snippets.

@levelsio
Created November 27, 2016 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save levelsio/0575b7bfebea918e50a267af9b9168c8 to your computer and use it in GitHub Desktop.
Save levelsio/0575b7bfebea918e50a267af9b9168c8 to your computer and use it in GitHub Desktop.
$('.action-upload-file').bind('click',function() {
$('.upload-file-uploader').trigger('click');
});
$('.upload-file-uploader').bind('change',function(evt) {
var files = evt.target.files; // FileList object
console.log(files);
// files is a FileList of File objects. List some properties.
var output = [];
var imageAsText='';
for (var i = 0, f; f = files[i]; i++) {
console.log(i);
var reader = new FileReader();
reader.readAsDataURL(f);
reader.onload = function(e) {
imageAsBase64=e.target.result;
array=explode(',',imageAsBase64);
imageAsBase64=array[1];
$('.chat-box .notice').html('Uploading file');
$('.chat-box .notice').show();
if(typeof addDotsToNoticeInterval !== 'undefined') clearInterval(addDotsToNoticeInterval);
addDotsToNoticeInterval=setInterval(function() {
$('.chat-box .notice').html(str_replace('....','',$('.chat-box .notice').html()+'.'));
},500);
// send to imgur
$.ajax({
url: "https://api.imgur.com/3/upload",
headers: {
'authorization':'Client-ID 54d591aedeba2d4'
},
type:'POST',
data:{
image:imageAsBase64
},
context: document.body,
error: function (xhr, status, error) {
$('.chat-box .notice').text("Couldn't upload image: "+error);
clearInterval(addDotsToNoticeInterval);
},
success: function (reply) {
$('.chat-box .notice').hide();
clearInterval(addDotsToNoticeInterval);
imgUrl=reply.data.link;
sendMessage='@'+selfUsername+' uploaded a file: '+reply.data.link;
$.ajax({
url: "/chat",
type:'PUT',
dataType:'json',
data:{
action:'message-channel',
channel:activeChannel,
message:sendMessage,
team:activeTeam,
epoch:epoch,
private:private,
earliestEpoch:earliestChatEpoch,
latestEpoch:latestChatEpoch,
recipient_user_id:recipient_user_id
},
context: document.body
}).done(function(reply) {
consoleNotlog(reply);
// we receive back new messages after sending, process them
processRefreshChatReply(reply);
});
}
});
};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment