Handle uploads using Ctrl-V

This commit is contained in:
Christoph Haas 2021-02-18 01:59:40 +01:00
parent 499c59b761
commit 1d364de8c8
3 changed files with 48 additions and 2 deletions

View file

@ -54,3 +54,42 @@ upload_form_files_selected = function() {
$('#file-form').submit(); // start upload
}
};
function upload_paste(event){
console.log("paste!");
var items = (event.clipboardData || event.originalEvent.clipboardData).items;
// /*Make Sure Only One File is Copied*/
// if (items.length != 1) {
// return;
// }
for (var i = 0 ; i < items.length ; i++) {
var item = items[i];
if (!item.type.startsWith('image/')){ continue }
var file = item.getAsFile();
console.log(file);
upload_file_with_ajax(file);
}
}
function upload_file_with_ajax(file){
var formData = new FormData();
formData.append('file', file);
$.ajax('#{{upload_receive_path}}' , {
type: 'POST',
contentType: false,
processData: false,
data: formData,
dataType: 'text',
error: function(xhr, txt, err) {
console.log("error: "+txt+" / "+err);
},
success: function(res) {
console.log("AJAX file upload was ok");
}
});
}

View file

@ -59,7 +59,12 @@ class PackagesController < ApplicationController
all_errors = []
params[:file].each do |img|
files = params[:file]
# Turn into array if a single image was uploaded through AJAX
files=[files] if files.class != Array
files.each do |img|
# Log.log "Uploaded img=#{img.path}"
new_screenshot = @package.screenshots.new(simage: img)

View file

@ -66,4 +66,6 @@ h1 = "Upload screenshots for #{@package.name}"
javascript:
// Hide the submit button unless images are selected for upload
$(document).ready(upload_form_init);
// TODO: Move into javascripts/packages/upload...
// Handle Ctrl-V to paste an image directly
document.onpaste = upload_paste;