HTML5
<a id="up_status" onclick="$('#input_frm').click();">Thêm ảnh</a> <input style="display:none" type="file" onchange="upload(this.files[0])" id="input_frm"> <input type="hidden" id="data_image"> <div class="data" style="display:block"></div>
JAVASCRIPT
function upload(file){ if(file.type.match(/image.*/)){ var fd=new FormData; fd.append("image",file); var xhr=new XMLHttpRequest; $("#up_status").html("Đang tải lên"); xhr.open("POST","http://rhash.tk/upload.php?domain=site"); $(".data").append('<div class="image"><div class="progress"><div class="bar_load"></div></div></div>'); xhr.upload.onprogress=function(t){ $(".image:eq(-1) .bar_load").attr("style","width:"+t.loaded/t.total*100+"%")} xhr.onload=function(){ var t=JSON.parse(xhr.responseText); $(".image:eq(-1)").append('<img src="'+t.link+' " >'),$(".image:eq(-1) img").load(function(){ $(".image:eq(-1) .progress").fadeOut(); $("#data_image").val($("#data_image").val()+"[img]"+t.link+"[/img]"),$("#up_status").html("Thêm ảnh"); $("#input_frm").val("") }); } xhr.send(fd); } }
CSS
.data{ display:block } .image{ width: 60px; height: 60px; border: 1px solid #aaa; position: relative; background: #eee; display: inline-block; margin-right: 3px; } .image.complete .progress{ display: none; } .progress{ border: 1px solid #ddd; height: 10px; position: absolute; bottom: 3px; left: 3px; right: 3px; } .bar_load{ background: #00dcff; height: 100%; width: 0%; transition: 0.2s all; box-shadow: -2px 0px 3px -1px #000 inset; } .image img{ height: 100%; width: 100%; max-height: 100%; }
ở http://rhash.tk/upload.php?domain=site qua rhash.tk reg wap bạn rùi sửa vào