I've been trying to implement image upload with the following requirements:
- Drag and drop
- Display dropped image in a popup with option to resize image
- Upload image after preview and resize
I'm trying to restrict my options to either bass jobsen's jqueryupload. Using this plugin, I've so far managed to do something similar to this:
$('document).on('drop', '#drop_area', function(event) {
var file_input = $('<input>').attr({type: 'file', 'id': 'hidden_file_input'});
$('body').append(file_input.hide());
var file = event.originalEvent.dataTransfer.files[0];
var file_reader = new FileReader();
file_reader.onloadend = function(e) {
var src = file_reader.result;
file_upload_preview();
}
file_reader.readAsDataUrl(file);
$file_input[0][files][0] = file; // this line only works 5% of the time
});
function file_upload_preview() {
self.modal_show('/modal/preview', function(e) { // render popup using file /modal/preview.html
// do image crop options
});
doUpload();
}
function doUpload() {
var file_input = $('#upload_form_id');
var file = file_input.get(0).files[0]; // throws error because of $file_input[0][files][0] = file; not working
var url = '/tmp/uploads';
var data = {
'x' : file_input.data('x') // image resize dimension
// add others, etc.
};
// do validations
file_input.upload(url, data, function(json) { // jqueryupload start
var res = JSON.parse(json); // throws error if the above code doesn't
});
}
I am getting multiple errors with this code:
- The line
$file_input[0][files][0] = file;
will not always work for some reason - If #1 does not happen, another error is thrown when trying to upload: "Can not process empty Imagick object"
What is the best and sure way I can set the value of file_input
by drag and drop instead of choosing the file? Also is it possible to implement what I need using this plugin?