移动端图片上传:localResizeIMG先压缩再ajax上传

移动端图片上传:localResizeIMG先压缩再ajax上传

 

下载https://github.com/think2011/localResizeIMG/archive/4.9.35.zip,之后引入以下几个js文件以及jquery.js

1.html

<div class="feedback">
	<div class="mui-col-xs-12 mui-text-center">

		<h3 style="margin: 25px;">上传图片测试</h3>

		<div id="image-list" class="row image-list"></div>

	</div>
</div>

2.js

var customArr = [{
	name: "IdCodeFront",
	text: "身份证正面",
	value: "1"
}, {
	name: "IdCodeBack",
	text: "身份证反面",
	value: "2"
}];
$(function() {
	var str = "";
	for(var i = 0; i < customArr.length; i++) {
		str += ("<div class=\'image-item space\'>");
		str += ("	<div class=\'image-close close-" + customArr[i].value + "\'>×</div>");
		str += ("	<div class=\'customText\'>" + customArr[i].text + "</div>");
		str += ("	<input type=\'file\' accept=\'image/*\' id=\'image-" + customArr[i].value + "\'>");
		str += ("</div>");
	}
	$("#image-list").html(str);
	init();
})

function init() {
	$('#image-list').on('change', 'input[type=file]', function() {
		var that = this;
		lrz(that.files[0], {
				width: 640
			})
			.then(function(rst) {
//ajax上传
				$(that).parent().css("background-image", "url(" + rst.base64 + ")").removeClass("space");

			})
			.catch(function(err) {

			})
			.always(function() {

			});
	});
	$('#image-list').on('click', '.image-close', function() {
		var that = this;
		$(that).parent().css("background-image", "").addClass("space");
	})
}

其中可以设置参数:

lrz(file, {
    width:640,//图片最大不超过的宽度,默认为原图宽度,高度不设时会适应宽度。
    height:640,//参考宽度
    quality :0.7,//图片压缩质量,取值 0 - 1,默认为0.7
    fieldName:file//后端接收的字段名,默认:file
});

效果:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Let dreams fly

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值