AJAX Multi-File Upload With Image Preview and Sort [Under Development]
I was looking into making this possible but I got stuck on the way.
1 | <input type = "file" /> |
I believe the above code is the culprit on this Project since the way it store the files is randomized, therefore making it almost impossible to re-sort the images inside it.
The Codes bellow will work most of the time, and sometimes it will fail due to queueing issues on the input file.
Client Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Upload images with Jquery</title> <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.3.0/bootbox.min.js"></script> <script type="text/javascript" src="jquery-2.1.3.min.js"></script> <script type="text/javascript" src="jquery-ui.js"></script> <style> ul.cvf_uploaded_files {list-style-type: none; margin: 20px 0 0 0; padding: 0;} ul.cvf_uploaded_files li {background-color: #fff; border: 1px solid #ccc; border-radius: 5px; float: left; margin: 20px 20px 0 0; padding: 2px; width: 150px; height: 150px; line-height: 150px; position: relative;} ul.cvf_uploaded_files li img.img-thumb {width: 150px; height: 150px;} ul.cvf_uploaded_files .ui-selected {background: red;} ul.cvf_uploaded_files .highlight {border: 1px dashed #000; width: 150px; background-color: #ccc; border-radius: 5px;} ul.cvf_uploaded_files .delete-btn {width: 24px; border: 0; position: absolute; top: -12px; right: -14px;} .bg-success {padding: 7px;} </style> <script type="text/javascript"> jQuery(document).ready(function() { var storedFiles = []; //$('.cvf_order').hide(); // Apply sort function function cvf_reload_order() { var order = $('.cvf_uploaded_files').sortable('toArray', {attribute: 'item'}); $('.cvf_hidden_field').val(order); } function cvf_add_order() { $('.cvf_uploaded_files li').each(function(n) { $(this).attr('item', n); }); console.log('test'); } $(function() { $('.cvf_uploaded_files').sortable({ cursor: 'move', placeholder: 'highlight', start: function (event, ui) { ui.item.toggleClass('highlight'); }, stop: function (event, ui) { ui.item.toggleClass('highlight'); }, update: function () { //cvf_reload_order(); }, create:function(){ var list = this; resize = function(){ $(list).css('height','auto'); $(list).height($(list).height()); }; $(list).height($(list).height()); $(list).find('img').load(resize).error(resize); } }); $('.cvf_uploaded_files').disableSelection(); }); $('body').on('change', '.user_picked_files', function() { var files = this.files; var i = 0; for (i = 0; i < files.length; i++) { var readImg = new FileReader(); var file = files[i]; if (file.type.match('image.*')){ storedFiles.push(file); readImg.onload = (function(file) { return function(e) { $('.cvf_uploaded_files').append( "<li file = '" + file.name + "'>" + "<img class = 'img-thumb' src = '" + e.target.result + "' />" + "<a href = '#' class = 'cvf_delete_image' title = 'Cancel'><img class = 'delete-btn' src = 'delete-btn.png' /></a>" + "</li>" ); }; })(file); readImg.readAsDataURL(file); } else { alert('the file '+ file.name + ' is not an image<br/>'); } if(files.length === (i+1)){ setTimeout(function(){ cvf_add_order(); }, 1000); } } }); // Delete Image from Queue $('body').on('click','a.cvf_delete_image',function(e){ e.preventDefault(); $(this).parent().remove(''); var file = $(this).parent().attr('file'); for(var i = 0; i < storedFiles.length; i++) { if(storedFiles[i].name == file) { storedFiles.splice(i, 1); break; } } //cvf_reload_order(); }); // AJAX Upload $('body').on('click', '.cvf_upload_btn', function(e){ e.preventDefault(); cvf_reload_order(); //$(".cvf_uploaded_files").html('<p><img src = "loading.gif" class = "loader" /></p>'); var data = new FormData(); var items_array = $('.cvf_hidden_field').val(); var items = items_array.split(','); for (var i in items){ var item_number = items[i]; data.append('files' + i, storedFiles[item_number]); } $.ajax({ url: 'upload.php', type: 'POST', contentType: false, data: data, processData: false, cache: false, success: function(response, textStatus, jqXHR) { //$(".cvf_uploaded_files").html(''); //bootbox.alert('<br /><p class = "bg-success">File(s) uploaded successfully.</p>'); alert(jqXHR.responseText); } }); }); }); </script> </head> <body> <div class = "container-fluid"> <h3>Sort Files before upload</h3> <br /> <div class = "col-md-6"> <div class = "form-group"> <label>Select Images</label> <input type = "file" name = "upload" multiple = "multiple" class = "form-control user_picked_files" /> </div> <div class = "form-group cvf_order"> <label>Order</label> <input type = "text" class = "form-control cvf_hidden_field" value = "" disabled = "disabled" /> </div> <input type = "submit" class = "cvf_upload_btn btn btn-primary" value = "Upload" /> <ul class = "cvf_uploaded_files"></ul> </div> </div> </body> </html> |
Server Script (upload.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php if(isset($_FILES)){ $i = 1; foreach($_FILES as $key => $data){ if (move_uploaded_file($data['tmp_name'], __DIR__ . '/uploads/' . $i . '--' . $data['name'])) { //echo "success"; print_r($data['name']); } else { //echo "error"; } $i++; } } |
Do you need help with a project? or have a new project in mind that you need help with?
Contact Me