diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index b419b6f..1a4a0b7 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -54,42 +54,3 @@ 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"); - } - }); -} diff --git a/app/views/packages/upload.slim b/app/views/packages/upload.slim index 9d91dd9..0803605 100644 --- a/app/views/packages/upload.slim +++ b/app/views/packages/upload.slim @@ -68,4 +68,59 @@ javascript: $(document).ready(upload_form_init); // Handle Ctrl-V to paste an image directly - document.onpaste = upload_paste; + document.onpaste = function (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]; + console.log(item); + console.log("item size="+item.size); + if (!item.type.startsWith('image/')) + { + console.log("Skipping file with type "+item.type); + continue + } + console.log(item.type); + var file = item.getAsFile(); + console.log(file); + upload_file_with_ajax(file); + } + } + + function upload_file_with_ajax(file) + { + if (file==null) + { + console.log("File was null. Strange."); + return; + } + var formData = new FormData(); + formData.append('file', file); + + $.ajax('#{{upload_receive_path(format: :json)}}' , + { + type: 'POST', + contentType: false, + processData: false, + data: formData, + //dataType: 'json', + error: function(xhr, txt, err) + { + console.log("AJAX file upload returned error: "+txt+" / "+err); + }, + success: function(res) + { + console.log("AJAX file upload was ok"); + // Go back to details page to show the new upload + window.location.replace("#{{package_path(@package)}}"); + } + }); + }