js 调用后台,后台调用js
<html xmlns="http://www.w3.org/1999/xhtml">
<head />
</div>
</form>
</body>
</html>
js 压缩图片
<script type="text/javascript">
$(document).ready(function () {
var BH = $('.file-wrap img').height();
function fileChange(e) {
var f = e.files[0];
var _size = f.size;
if (_size > 2048 * 1024) {
mui.alert('选择的图片超过限度,请重新选择!', '', function () { });
return;
}
//console.log(f);
var FR = new FileReader();
FR.readAsDataURL(f);
FR.onload = function (f) {
compressImg(this.result, BH * 2, function (data) {
//$("#newImg").attr("src",data);//保存图片压缩后的64位编码
$('.file-wrap img').attr('src', data);
});
};
}
document.getElementById("image").addEventListener("change", function () {
fileChange(this);
}, false);
function compressImg(imgData, maxHeight, onCompress) {
if (!imgData) { return false };
onCompress = onCompress || function () { };
maxHeight = maxHeight || 100; //默认最大高度1000px
var canvas = document.createElement('canvas');
var img = new Image();
img.src = imgData;
img.onload = function () {
if (img.height > maxHeight) {//按最大高度等比缩放
img.width *= maxHeight / img.height;
img.height = maxHeight;
}
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0, img.width, img.height);
onCompress(canvas.toDataURL("image/jpeg"));
//console.log(canvas.toDataURL("image/jpeg"));
$('#IMAGE_URL').val(canvas.toDataURL("image/