Moved ctrl-v upload Javascript back to controller
This commit is contained in:
parent
aca9df2910
commit
afe6f7ed7f
2 changed files with 56 additions and 40 deletions
|
|
@ -54,42 +54,3 @@ upload_form_files_selected = function() {
|
||||||
$('#file-form').submit(); // start upload
|
$('#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");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -68,4 +68,59 @@ javascript:
|
||||||
$(document).ready(upload_form_init);
|
$(document).ready(upload_form_init);
|
||||||
|
|
||||||
// Handle Ctrl-V to paste an image directly
|
// 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)}}");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue