Skip to content

Instantly share code, notes, and snippets.

@stefanolaru
Created January 15, 2019 18:34
Show Gist options
  • Save stefanolaru/32f951c87391b0cad60e6be1b508d7f9 to your computer and use it in GitHub Desktop.
Save stefanolaru/32f951c87391b0cad60e6be1b508d7f9 to your computer and use it in GitHub Desktop.
Dropzone get signed request and upload to S3
var dz = new Dropzone('#dropzone-uploader', {
url: '/admin/media/upload',
acceptedFiles: 'image/jpeg',
method: 'put',
maxFilesize: 2,
parallelUploads: 1,
uploadMultiple: false,
createImageThumbnails: false,
autoProcessQueue: false,
previewTemplate: $('#preview-template').html(),
accept: function(file, done) {
var url = $.post('/admin/media/get_s3_signed_request', {
filePath: file.name,
contentType: file.type,
fileSize: file.size
})
.done(function(res) {
// console.log(res);
file.uploadURL = res.url;
file.uploadKey = res.key;
done();
//
setTimeout(function() {
dz.processFile(file);
});
})
.fail(function(err) {
console.log(err);
return err;
});
},
sending: function(file, xhr) {
var _send = xhr.send;
xhr.send = function() {
_send.call(xhr, file);
}
},
success: function(file, response){
var req = $.post('/admin/media/save', {
's3_path': file.uploadKey
})
.done(function(res) {
var tpl = '<div class="new-image" data-id="'+res.id+'">';
tpl += '<img src="'+res.src+'" alt="thumbnail preview" />';
tpl += '<input type="hidden" name="images[]" value="'+res.id+'" />';
tpl += '<span class="delete">&#215;</span>';
tpl += '</div>';
$('#uploaded-images').append(tpl);
});
},
processing: function(file) {
this.options.url = file.uploadURL;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment