Drag and drop JS
(function ( $ ) {
$.fn.imageUpload = function( options ) {
var settings = $.extend({
outputHandler: function() {}, // Callback function for processing output. Parameter is image source in base64 format
onBeforeUpload: function() {},// Callback function called before upload processing
onAfterUpload: function() {}, // Callback function called after upload processing
maxUpload: 113, // Max number of image files allowed to be uploaded.
errorMsg: 'Error: The file you just uploaded is not an image!',
trigger: 'click', // possible values: "click", "dblclick" or null
errorContainer: null, // Container for error message
enableCliboardCapture: true, // Enable clipboard capturing
enableDebug: true // enable debugging output in console
}, options );
var $this = $(this), dragging = false, pasting = false;
var $uploadField = $('<input type="file" style="display:none" name="fileselect[]" multiple="multiple" accept="image/*, .gif, .png, .jpg" />');
// Create capture area for Mozilla & IE
var $contentEditable = $('<div contenteditable="true" style="position:absolute;overflow:hidden;top:-20px;width:0;height:0;" />');
// Error display
var errorHandler = function(error) {
if (error && settings.errorContainer !== null) {
settings.errorContainer.html(settings.errorMsg).fadeIn(1000,function() {
$(this).delay(5000).fadeOut(1000);
});
}
};
var readFile = function(file) {
var reader = new FileReader();
$(reader).on('load', file, function(e){
var returnValue = {
name: (e.data.name ? e.data.name :'CB_Image_' + (+new Date()) + getFileExtension(e.target.result)),
type: (e.data.type ? e.data.type : getFileType(e.target.result)),
size: (e.data.size ? e.data.size : 'n/a'),
imgSrc: e.target.result
};
// Execute outputHandler callback function if available
$.isFunction(settings.outputHandler) && settings.outputHandler.call(this, returnValue);
// Execute onAfterUpload callback function if available
$.isFunction(settings.onAfterUpload) && settings.onAfterUpload.call(this);
});
reader.readAsDataURL(file);
};
// Upload processing
var processUpload = function(files, originalEvent) {
var error = false, imageFound = false, count = 0;
console.log(event.dataTransfer.files[0].name);
if (files !== null) {
for (var i = 0; i < files.length; i++) {
if (files[i].type.indexOf("image") == -1) {
//not image
error = true;
continue;
}
if (count == settings.maxUpload) break; // Max reached
count++;
imageFound = true;
// Webkit clipboard data needs data converted through getAsFile()
var blob = (typeof originalEvent !== 'undefined' && !originalEvent.msConvertURL) ? files[i].getAsFile() : files[i];
readFile(blob);
}
if (!imageFound) {
// Execute onAfterUpload callback function if available
$.isFunction(settings.onAfterUpload) && settings.onAfterUpload.call(this);
}
errorHandler(error);
}
};
// Paste handler
var pasteHandlerFunc = function(e) {
console.log(e.originalEvent.dataTransfer.files[0].name);
// Remove existing Mozilla "input" event handler
$contentEditable.off('input',mozillaCaptureFunc);
// Execute onBeforeUpload callback function if available
$.isFunction(settings.onBeforeUpload) && settings.onBeforeUpload.call(this);
var clipboardData = e.originalEvent.clipboardData || window.clipboardData;
if (clipboardData === false || clipboardData === undefined) {
console.log(e.originalEvent.dataTransfer.files[0].name);
// empty
// Execute onAfterUpload callback function if available
$.isFunction(settings.onAfterUpload) && settings.onAfterUpload.call(this);
pasting = false;
return false;
}
var files = clipboardData.items || clipboardData.files;
// No Items or files, so either nothing in clipboard or Mozilla browser
if (files.length === 0) {
console.log(e.originalEvent.dataTransfer.files[0].name);
// Enable "input" event handler for Mozilla, and set focus
$contentEditable.on('input',mozillaCaptureFunc).focus();
}
else {
console.log(e.originalEvent.dataTransfer.files[0].name);
pasting = false;
processUpload(files, e.originalEvent);
e.preventDefault();
}
};
// Alternate triggering metod for clipboard capturing, required for IE & Mozilla
var keydownHandlerFunc = function(e) {
if (!pasting && (e.ctrlKey || e.metaKey) && e.which == 86) {
console.log(e.originalEvent.dataTransfer.files[0].name);
pasting = true;
$contentEditable.focus();
}
};
// Alternative to css :hover, adding class "dropzonehover" to body tag.
var mouseenterHandlerFunc = function(e) {
e.preventDefault();
if (!$('body').hasClass('dragging')) $('body').addClass('dropzonehover');
};
var mouseleaveHandlerFunc = function(e) {
e.preventDefault();
$('body').removeClass('dropzonehover');
};
// Submit handler to trigger hidden 'input type="file"' upload field
var submitFunc = function(e) {
e.preventDefault();
$uploadField.trigger('click');
};
// File upload handler
var uploadFunc = function(e) {
e.preventDefault();
processUpload(e.target.files);
};
// "drop" handler
var dropHandlerFunc = function(e) {
e.preventDefault();
$('body').removeClass('dragging dragover');
dragging = false;
processUpload(e.originalEvent.dataTransfer.files);
};
// General handler for preventing propragation and default action
var preventHandlerFunc = function(e) {
e.preventDefault();
e.stopPropagation();
};
// Dragover handler for preventing propragation and default action
var preventDragoverHandlerFunc = function(e) {
e.preventDefault();
e.stopPropagation();
e.originalEvent.dataTransfer.dropEffect = 'none';
try {
// IE won't allow setting of effectAllowed
e.originalEvent.dataTransfer.effectAllowed = 'none';
} catch(err) {}
};
// "dragover" handler
var dragoverHandlerFunc = function(e) {
e.preventDefault();
e.stopPropagation();
e.originalEvent.dataTransfer.dropEffect = 'copy';
try {
// IE won't allow setting of effectAllowed
e.originalEvent.dataTransfer.effectAllowed = 'copyMove';
} catch(err) {}
};
// "dragenter" handler, adding class "dragover" to body tag.
var dragenterHandlerFunc = function(e) {
e.preventDefault();
$('body').addClass('dragover');
};
// "dragleave" handler, removing class "dragover" from body tag.
var dragleaveHandlerFunc = function(e) {
e.preventDefault();
$('body').removeClass('dragover');
};
// Pseudo "dragstart" handler, adding class "dragging" to body tag.
var dragstartHandlerFunc = function(e) {
e.preventDefault();
e.stopPropagation();
if (!$('body').hasClass('dragging') && !dragging) {
$('body').addClass('dragging');
dragging = true;
}
};
// Pseudo "dragend" handler, removing class "dragging" from body tag.
var dragendHandlerFunc = function(e) {
e.preventDefault();
e.stopPropagation();
if ($('body').hasClass('dragging') && dragging) {
$('body').removeClass('dragging');
dragging = false;
}
};
// Enable clipboard capturing
if (settings.enableCliboardCapture) {
$contentEditable.appendTo('body').focus();
$(window).on({
keydown: keydownHandlerFunc, // IE & Mozilla
paste: pasteHandlerFunc
});
}
// Enable file requester upload
if (settings.trigger !== null) {
$uploadField.on('change',uploadFunc).appendTo('body').blur();
$this.on(settings.trigger, submitFunc);
}
// Enable drag and drop upload
$this.on({
dragenter: dragenterHandlerFunc,
dragleave: dragleaveHandlerFunc,
dragover: dragoverHandlerFunc,
drop: dropHandlerFunc,
mouseenter: mouseenterHandlerFunc,
mouseleave: mouseleaveHandlerFunc
});
// Prevent dropping outside droparea
// Since dragstart and dragend is not triggered when dragging local files,
// the "html" tag is used as fake "triggers" (Using "body" tag has problems)
$('html').on({
dragbetterenter: dragstartHandlerFunc,
dragbetterleave: dragendHandlerFunc,
dragover: preventDragoverHandlerFunc,
drag: preventHandlerFunc,
drop: preventHandlerFunc
});
// chainable
return this;
};
}( jQuery ));
jQuery(document).ready(function($) {
// Shared callback handler for processing output
var outputHandlerFunc = function(imgObj) {
var sizeInKB = function(bytes) {return (typeof bytes == 'number') ? (bytes/1024).toFixed(2) + 'Kb' : bytes;};
var getThumbnail = function(original, maxWidth, maxHeight, upscale) {
var canvas = document.createElement("canvas"), width, height;
if (original.width<maxWidth && original.height<maxHeight && upscale == undefined) {
width = original.width;
height = original.height;
}
else {
width = maxWidth;
height = parseInt(original.height*(maxWidth/original.width));
if (height>maxHeight) {
height = maxHeight;
width = parseInt(original.width*(maxHeight/original.height));
}
}
canvas.width = width;
canvas.height = height;
canvas.getContext("2d").drawImage(original, 0, 0, width, height);
$(canvas).attr('title','Original size: ' + original.width + 'x' + original.height);
return canvas;
}
$(new Image()).on('load', function(e) {
var $wrapper = $('<li class="new-item"><div class="list-content"><span class="preview"></span><span class="type">' + imgObj.name + '<br>' + (e.target.width + '×' + e.target.height) + '<br>' + sizeInKB(imgObj.size) + '</span><span class="name"><span class="imagedelete" title="Remove image"></span><span class="button"<button>рассчитать</button></span>' +'</span><span class="options"><span class="imagedelete" title="Remove image"></span><span class="options"></span></div></li>').appendTo('#output ul');
$('.button',$wrapper).one('click',function(e) {console.log('')});
$('.imagedelete',$wrapper).one('click',function(e) {
$wrapper.toggleClass('new-item').addClass('removed-item');
$wrapper.one('animationend webkitAnimationEnd MSAnimationEnd oAnimationEnd', function(e) {
$wrapper.remove();
});
});
var thumb = getThumbnail(e.target,50,50);
var $link = $('<a rel="fancybox">').attr({
target:"_blank",
href: imgObj.imgSrc
}).append(thumb).appendTo($('.preview', $wrapper));
}).attr('src',imgObj.imgSrc);
}
$("a[rel=fancybox]").fancybox();
var fileReaderAvailable = (typeof FileReader !== "undefined");
var clipBoardAvailable = (window.clipboardData !== false);
var pasteAvailable = Boolean(clipBoardAvailable & fileReaderAvailable & !eval('/*@cc_on !@*/false'));
if (fileReaderAvailable) {
// Enable drop area upload
$('#dropzone').imageUpload({
errorContainer: $('span','#errormessages'),
trigger: 'dblclick',
enableCliboardCapture: pasteAvailable,
onBeforeUpload: function() {$('body').css('background-color','green');},
onAfterUpload: function() {$('body').css('background-color','#eee');},
outputHandler:outputHandlerFunc
})
$('#dropzone').prev('#textbox-wrapper').find('#textbox').append('<p class="large">Drag and Drop<br>Image File here</p><p class="small">Doubleclick<br>for file requester</p>');
}
else {
$('body').addClass('nofilereader');
}
if (!pasteAvailable) {
$('body').addClass('nopaste');
}
});
body {
background: #eee;
font: normal 11px arial;
}
kbd {
background-color: #eee;
border: 1px solid gray;
display: inline-block;
font-size: 1.2em;
line-height: 1.4;
box-shadow: 1px 0 1px 0 #eee, 0 2px 0 2px #ccc, 0 2px 0 3px #444;
border-radius: 3px;
margin: 6px 3px;
padding: 1px 5px;
}
#errormessages {
font: bold 13px Arial;
color: #DD3030;
min-height: 32px;
padding: 0px 10px;
text-align: center;
}
#dropzone-wrapper {
background-color: #fff;
margin: 10px;
border: 6px dotted #ddd;
border-radius: 10px;
height: 200px;
width: 200px;
position: relative;
cursor: pointer;
transition: background-color 0.3s ease-in-out, border-color 0.3s ease-in-out;
-webkit-touch-callout: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
#dropzone-wrapper #dropzone {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#dropzone-wrapper #textbox-wrapper {
display: table;
width: 100%;
height: 100%;
}
#dropzone-wrapper #textbox-wrapper #textbox {
display: table-cell;
text-align: center;
vertical-align: middle;
}
#dropzone-wrapper #textbox-wrapper #textbox p {
transition: all 0.5s ease-in-out;
margin: 0 0 10px 0;
font: bold 20px Arial;
text-shadow: 0 1px 1px rgba(255, 255, 255, 0.42);
}
#dropzone-wrapper #textbox-wrapper #textbox p.small {
color: #777;
font: bold 12px Arial;
}
/* Replacement for :hover */
.dropzonehover #dropzone-wrapper {
background-color: rgba(220, 109, 10, 0.2);
border-color: #dc6d0a;
}
.dragover #dropzone-wrapper {
background-color: #9bf79b;
border-color: #006d0a;
}
.dropzonehover #dropzone-wrapper #textbox-wrapper #textbox p.small {
color: #dc6d0a;
font-size: 21px;
}
.dropzonehover #dropzone-wrapper #textbox-wrapper #textbox p.large {
font-size: 0px;
transform: scale(0);
}
.dragging #dropzone-wrapper #textbox-wrapper #textbox p.small {
font-size: 0px;
transform: scale(0);
}
.dragging #dropzone-wrapper #textbox-wrapper #textbox p.large {
color: green;
font-size: 21px;
}
.dragover #dropzone-wrapper #textbox-wrapper #textbox p.small {
color: red;
}
.dragging #dropzone-wrapper {
z-index: 99999;
-webkit-animation: greenPulse 1.5s infinite;
animation: greenPulse 1.5s infinite;
}
#output canvas {
margin: 5px;
border: 1px solid #ddd;
box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.5);
}
#overlay {
background: rgba(0, 0, 0, 0);
position: fixed;
left: 0;
top: 0;
z-index: -99998;
width: 100%;
height: 100%;
display: none;
}
.dragging #overlay {
z-index: 99998;
display: block;
background: rgba(0, 0, 0, 0.5);
}
@-webkit-keyframes greenPulse {
from {
box-shadow: 0 0 4px #08FF00, 0 0 6px #fff;
border-color: #aaa;
}
50% {
box-shadow: 0 0 22px #0FBC09, 0 0 30px #fff;
border-color: #0FBC09;
}
to {
box-shadow: 0 0 9px #08FF00, 0 0 6px #fff;
border-color: #aaa;
}
}
@keyframes greenPulse {
from {
box-shadow: 0 0 4px #08FF00, 0 0 6px #fff;
border-color: #aaa;
}
50% {
box-shadow: 0 0 22px #0FBC09, 0 0 30px #fff;
border-color: #0FBC09;
}
to {
box-shadow: 0 0 9px #08FF00, 0 0 6px #fff;
border-color: #aaa;
}
}
#helpbutton {
background: #eee;
background: linear-gradient(#eee, #e2e2e2);
border: solid 1px #d0d0d0;
border-bottom: solid 3px #b2b1b1;
border-radius: 3px;
box-shadow: inset 0 0 0 1px #f5f5f5;
color: #555;
cursor: pointer;
display: inline-block;
font: bold 12px Arial, Helvetica, Clean, sans-serif;
margin: 0 0 0 25px;
padding: 10px 20px;
position: relative;
text-align: center;
text-decoration: none;
text-shadow: 0 1px 0 #fafafa;
}
#helpbutton:hover {
background: #e4e4e4;
background: linear-gradient(#e4e4e4, #ededed);
border: solid 1px #c2c2c2;
border-bottom: solid 3px #b2b1b1;
box-shadow: inset 0 0 0 1px #efefef;
}
#helpbutton #helpscreen {
color: #111;
text-align: left;
position: absolute;
top: -1px;
left: -1px;
width: 0px;
height: 0px;
padding: 0px;
border: 1px solid #E8E8E8;
background: #ffff88;
transition: all 0.2s ease-in-out;
font: normal 11px arial;
overflow: hidden;
}
#helpbutton:active #helpscreen, #helpbutton #helpscreen:hover {
width: 380px;
height: 280px;
padding: 10px 15px;
z-index: 10;
}
.nopaste #helpbutton, .nofilereader #dropzone-wrapper {
display: none;
}
#output ul {
max-width: 500px;
list-style: none;
margin: 0;
padding: 0;
}
#output ul li {
list-style: none;
margin: 0;
padding: 0;
}
.list-content {
display: table;
table-layout: fixed;
width: 100%;
background: #fff;
border: 1px solid #aaa;
margin: 5px 0;
}
.list-content span {
display: table-cell;
vertical-align: middle;
padding: 5px 10px;
}
.list-content span.preview {
width: 55px;
}
.list-content span.type {
text-align: center;
width: 60px;
}
.list-content span.name {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.list-content span.options {
width: 30px;
}
.list-content span.button {
background-color: #4CAF50; /* Green */
border: none;
color: white;
cursor: pointer;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
background-position: -106px 0px;
}
.list-content span.options .imagedelete {
cursor: pointer;
display: block;
float: right;
width: 16px;
height: 16px;
padding: 0;
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAQCAYAAAB3A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ywv2+rOyW+JEuPsmw9Zh/dg0zaMKn2JPpGFhDysGWA5Sy9u5ruNrjsfggwANaIwN5Hg9AjAAAAAElFTkSuQmCC");
background-position: 0px 0px;
}
.list-content span.options .imagedelete:hover {
background-position: -16px 0px;
}
#output .removed-item {
-webkit-animation: removed-item-animation 0.6s cubic-bezier(0.55, -0.04, 0.91, 0.94) forwards;
animation: removed-item-animation 0.6s cubic-bezier(0.55, -0.04, 0.91, 0.94) forwards;
}
#output .new-item {
opacity: 0;
-webkit-animation: openspace 0.6s ease forwards, new-item-animation 0.3s 0.3s cubic-bezier(0, 0.8, 0.32, 1.07) forwards;
animation: openspace 0.6s ease forwards, new-item-animation 0.3s 0.3s cubic-bezier(0, 0.8, 0.32, 1.07) forwards;
}
@-webkit-keyframes openspace {
to {
height: auto;
}
}
@keyframes openspace {
to {
height: auto;
}
}
@-webkit-keyframes removed-item-animation {
from {
opacity: 1;
transform: scale(1);
}
to {
opacity: 0;
transform: scale(0);
}
}
@keyframes removed-item-animation {
from {
opacity: 1;
transform: scale(1);
}
to {
opacity: 0;
transform: scale(0);
}
}
@-webkit-keyframes new-item-animation {
from {
opacity: 0;
transform: scale(0);
}
to {
opacity: 1;
transform: scale(1);
}
}
@keyframes new-item-animation {
from {
opacity: 0;
transform: scale(0);
}
to {
opacity: 1;
transform: scale(1);
}
}
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>CNN</title>
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.css'><link rel="stylesheet" href="./style.css">
</head>
<body>
<!-- partial:index.partial.html -->
<div style="float: left;width:240px;">
<div id="dropzone-wrapper">
<div id="textbox-wrapper"><div id=textbox></div></div>
<div id="dropzone"></div>
</div>
<div id="errormessages"><span style="display: none;"></span></div>
<div id="overlay"></div>
<div id="helpbutton">
<span>Paste Help</span>
<div id="helpscreen">
<p><strong>PC:</strong> Capture screen to clipboard: <kbd>PrtScr</kbd></p>
<p><strong>PC:</strong> Insert screenshot: <kbd>Ctrl</kbd> + <kbd>V</kbd></p>
<hr />
<!--<p>Capture screen to clipboard <kbd>Command</kbd> + <kbd>Shift</kbd> + <kbd>Control</kbd> + <kbd>3</kbd></p>-->
<p><strong>Mac:</strong> Capture screen to clipboard: <kbd>⌘</kbd> + <kbd>⇧</kbd> + <kbd>⌃</kbd> + <kbd>3</kbd></p>
<p><strong>Mac:</strong> Insert screenshot: <kbd>⌘</kbd> + <kbd>V</kbd></p>
<hr />
</div>
</div>
</div>
<div style="float: left;">
<br /><br />
<h3>Preview:</h3>
<div id="output"><ul></ul></div>
</div>
<div style="clear:both;"></div>
<!-- partial -->
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.js'></script>
<script src='https://cdn.jsdelivr.net/npm/[email protected]/jquery.dragbetter.min.js'></script><script src="./script.js"></script>
</body>
</html>
Всем привет, такой вопрос: сделал нейросеть на python, всегда пользовался консолью, а тут решил визуализировать. C js и ccs/html дел особо не имел, скачал код для drag and drop. Идея такая, закидываю снимки для классификации, нажимаю на кнопку, получаю результат. Все это дело я свяжу с ell, у меня уже почти все готово, единственный вопрос: как получить полный путь до файла с картинкой? У меня только имя и все, два часа мучался)) не знаю что делать, помогите)))