Skip to content

Instantly share code, notes, and snippets.

@osv
Created February 21, 2015 21:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osv/53eef63c59a3244a7e8f to your computer and use it in GitHub Desktop.
Save osv/53eef63c59a3244a7e8f to your computer and use it in GitHub Desktop.
Meteor drag drop upload
var uploadingPhotos = ReactiveVar(); // upload indicator
Template.portfolioPhoto.helpers({
uploading: function() { return uploadingPhotos.get(); },
moreThanOne: function() { return uploadingPhotos.get() > 1 ? true : false; }
});
function uploadFiles(files, profileId) {
_.each(files, function(file) {
if (!file.type.match(/^image\//)) {
// message..
return;
}
// some size check
if (file.size > 1200000) {
Messages.info('Файл "'+ file.name + '"слишком большой');
return;
}
// uploading..
var reader = new FileReader();
reader.onload = function(fileLoadEvent) {
uploadingPhotos.set((uploadingPhotos.get() || 0) +1);
Meteor.call('portfolio-upload', profileId, file.type, file.name, reader.result,
function(err) {
uploadingPhotos.set(uploadingPhotos.get()-1);
if (err) {
// message..
}
});
};
reader.readAsBinaryString(file);
});
}
Template.portfolioPhoto.events({
'click [data-action="cancel"]': function(){
editContactName.set(false);
},
'dragover #dropzone': function(e, t) {
e.preventDefault();
e.stopPropagation();
t.$('#dropzone').addClass('drag-over');
},
'dragleave #dropzone': function(e, t) {
e.preventDefault();
e.stopPropagation();
t.$('#dropzone').removeClass('drag-over');
},
'dragenter #dropzone': function(e, t) {
e.preventDefault();
e.stopPropagation();
},
'drop #dropzone': function(e, t) {
e.preventDefault();
e.stopPropagation();
uploadFiles(e.originalEvent.dataTransfer.files, this._id);
},
'change [type="file"]': function(event, template) {
uploadFiles(event.target.files, this._id);
},
});
<template name="portfolioPhoto">
{{#if uploading}}
<p class="form-control-static">
<div class="alert alert-info">
Loading
{{#if moreThanOne}}
{{uploading}} files left.
{{/if}}
</div>
</p>
{{>spinner}}
{{else}}
<div id="dropzone" class="dropzone active">
<div class="container">
DRAG IMAGE HERE or press
<span class="btn btn-file btn-primary">
upload <input type="file" multiple>
</span>
</div>
</div>
{{/if}}
</template>
.dropzone {
line-height:100px;
border:5px dashed @gray;
height: 300px;
background-color: @gray-lighter;
margin-bottom: @jumbotron-padding;
padding: @jumbotron-padding (@jumbotron-padding / 2);
font-size: ceil((@font-size-base * 1.5));
&.drag-over {
border:5px dashed green;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment