JS实现异步上传压缩图片

摘要: 使用iframe来处理异步上传图片,在现在这个时代来说,多多少少都有点落后了!单单就凭AJAX和JS就不能做到异步上传图片了吗?

感谢 think2011 这位兄台的JS库:

先看调用页面

<div class="jb51code">
<pre class="brush:xhtml;">
<!doctype html>

<Meta charset="utf-8"> <Meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no">
图片

<script type="text/javascript">
var img;
$("input:file").change(function (){
//console.log(this.files[0]);
lrz(this.files[0],{width:640,quality:0.9},function(rst){
img = rst.base64;
var html = [];
var show_img = new Image();
show_img.src = rst.base64;
$("#img_show").html("

");
$(".upimg").html(show_img);
});
});
$("#form").submit(function (){
var phone = $("input[name='phone']").val();
var month = $("input[name='month']").val();
$.post("upload.PHP",{img:img,phone:phone,month:month},function(data){
img = null;
alert(data.msg);
},'json');
return false;
});

1.首先你要载入JS类库:

2.然后就是写好form

3.准备处理图片以及图片异步提交的JS。

rush:js;">