Advertisement
asimryu

drag and drop & save in to localStorage example

Jul 6th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.25 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="ko">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Drag & Drop Images</title>
  6.     <style>
  7.         #dropbox {
  8.             width: 300px;
  9.             height: 300px;
  10.             border: 1px solid #888;
  11.             margin: 5px;
  12.         }
  13.         #imglist img {
  14.             width: 100px;
  15.             height: 100px;
  16.             margin: 5px;
  17.         }
  18.     </style>
  19. </head>
  20. <body>
  21.     <div class="wrap">
  22.         <div>
  23.             <button onclick="init()">초기화</button>
  24.             <button onclick="location.reload()">새로고침</button>
  25.         </div>
  26.         <div id="dropbox" ondrop="drop(event)" ondragover="dragover(event)" ondragleave="dragleave(event)">
  27.             Drop Images Here
  28.         </div>
  29.         <div id="imglist"></div>
  30.     </div>
  31.     <script src="http://code.jquery.com/jquery-latest.js"></script>
  32.     <script>
  33.         var imagedata = [];
  34.         function drop(e){
  35.             e.preventDefault();
  36.             var data = e.dataTransfer;
  37.             var files = data.files;
  38.  
  39.             if( files ) {
  40.                 for(i=0;i<files.length;i++){
  41.                     if( files[i].type == "image/jpeg" || files[i].type == "image/gif" || files[i].type == "image/png") {
  42.                         addimages(files[i]);
  43.                     }
  44.                 }
  45.             }
  46.         }
  47.  
  48.         function addimages(file){
  49.             var reader = new FileReader();
  50.             reader.addEventListener("load",function(){
  51.                 var tmpimg = "<img src='" + reader.result + "'>";
  52.                 $("#imglist").append(tmpimg);
  53.                 imagedata.push(reader.result);
  54.                 localStorage.setItem("imagedata",JSON.stringify(imagedata));
  55.             },false);
  56.             if( file ) reader.readAsDataURL(file);
  57.         }  
  58.        
  59.         function dragover(e){
  60.             e.preventDefault();
  61.             var tagetId = e.target.id;  document.getElementById(tagetId).style.background = "pink";
  62.         }  
  63.  
  64.         function dragleave(e){
  65.             e.preventDefault();
  66.             var tagetId = e.target.id;  document.getElementById(tagetId).style.background = "white";
  67.         }          
  68.        
  69.         window.onunload = function(){
  70.             //do something
  71.         }
  72.        
  73.         window.onload = function(){
  74.             var imagedataload = localStorage.getItem("imagedata");
  75.             if( imagedataload ) {
  76.                 var imgarr = JSON.parse(imagedataload);
  77.                 for( i=0; i<imgarr.length; i++){
  78.                     var tmpimg = "<img src='" + imgarr[i] + "'>";
  79.                     $("#imglist").append(tmpimg);
  80.                     imagedata.push(imgarr[i]);
  81.                 }
  82.             }
  83.         }
  84.  
  85.         function init(){
  86.             while (imagedata.length) { imagedata.pop(); }
  87.             localStorage.removeItem("imagedata");
  88.             $("#imglist").html("");
  89.         }
  90.     </script>
  91. </body>
  92. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement