$(‘.addWindowSave’).on(‘click’, function(){
var data = form.val(‘addWindowForm’);
data.content = tinyMCE.activeEditor.contentDocument.activeElement.innerHTML;
data.toyorno = top.ToyorNo;
var formdata = new FormData();
for(var p in data){
formdata.append(p,data[p]);
}
$.ajax({
type: ‘POST’,
url: ‘admin/other/saveOrAddOrderNews.do’,
data: formdata,
contentType: false, //必须
processData: false, //必须
success: function (res) {
if (res.success) {
layer.msg(‘保存成功’, {
icon: 1,
time: 1000 //1秒关闭(如果不配置,默认是3秒)
}, function(){
window.parent.reload();
window.parent.layer.closeAll()
});
} else {
layer.msg(res.msg)
}
}
});
});
===========================================================
{
xtype: ‘container’,
layout: ‘hbox’,
items: [
{
xtype: ‘label’,
forId: ‘’,
text: ‘图片1’,
margin: ‘10 0 0 50’
},
{
xtype: “button”,
name: ‘btnuploadpartimage1’,
text: ‘选择’,
glyph: 0xf093,
margin: ‘5 0 0 3’,
handler: ‘onUploadPic’
},
{
name: ‘upload_partimage1’,
xtype: ‘filefield’,
hidden: true,
listeners: {
“change”: “imageCompressUpload”,
afterrender: ‘onFilterFileType’
}
}, {
xtype: “hiddenfield”,
name: “partimage1”
}
]
}
onFilterFileType: function (obj, ops) {
$(Ext.query(“input[type=file][name=” + obj.name + “]”)[0]).attr(“accept”, “image/“);
},
//选择文件后预览
previewImage: function (obj, newValue, oldValue) {
if (!newValue) {
this.msgInfo(‘您只能选择jpg、jpeg、png、ico类型的图片,请选择合法的图片文件’);
return;
}
var index1 = newValue.lastIndexOf(“.”);
var index2 = newValue.length;
var suffix = newValue.substring(index1 + 1, index2);//后缀名
if (!suffix || (suffix.toLowerCase() != ‘jpg’ && suffix.toLowerCase() != ‘jpeg’ && suffix.toLowerCase() != ‘png’ && suffix.toLowerCase() != ‘ico’)) {
this.msgInfo(‘您只能选择jpg、jpeg、png、ico类型的图片,请选择合法的图片文件’);
return;
}
var file = Ext.query(“input[type=file][name*=” + obj.name + “]”)[0];
// var img = document.getElementById(previewImgId);
// Ext.get(obj.id.replace(‘upload‘,’’)).dom;也可以
//var img = Ext.query(“img[name=” + obj.id.replace(‘upload‘, ‘’) + “]”)[0];
//var img = Ext.ComponentQuery.query(‘box[name=’+obj.name.replace(‘upload‘, ‘’)+’]’)[0].el.dom;
var img = this.view.down(‘box[name=’ + obj.name.replace(‘upload_’, ‘’) + ‘]’).el.dom;
if (file.files && file.files[0]) {
var reader = new FileReader();
reader.onload = function (evt) {
img.src = evt.target.result;
}
reader.readAsDataURL(file.files[0]);
}
else //兼容IE
{
file.select();
var src = document.selection.createRange().text;
img.filters.item(‘DXImageTransform.Microsoft.AlphaImageLoader’).src = src;
}
},
imageCompressUpload: function (obj, newValue, oldValue) {//
var me = this;
me.previewImage(obj, newValue, oldValue);
var file = Ext.query(“input[type=file][name=” + obj.name + “]”)[0];
if (file.files && file.files[0]) {
me.msgWait(“正在上传图片,请稍候……”);
var reader = new FileReader();
var imgFile;
reader.onload = function (evt) {
if (me.showSize(evt.target.result) > 400 1024) {
me.dealImage(evt.target.result, {
// width: 1000,
quality: 0.7
}, function (base) {
me.showSize(base);
imgFile = me.dataURLtoFile(base, file.files[0].name);
me.upDataPartImageName(obj, imgFile);
});
} else {
imgFile = me.dataURLtoFile(evt.target.result, file.files[0].name);
me.upDataPartImageName(obj, imgFile);
}
};
reader.readAsDataURL(file.files[0]);
}
},
upDataPartImageName: function (obj, imgFile) {
var me = this;
var formdata = new FormData();
formdata.append(obj.name, imgFile);
formdata.append(‘toyorno’, top.ToyorNo);
formdata.append(‘partno’, obj.up(‘form’).getForm().findField(‘partno’).getValue());
$.ajax({
type: ‘post’,
url: ‘PartsInfo/updatePartImageName.do’,
data: formdata,
contentType: false, //必须
processData: false, //必须
timeout:7000,
success: function (data) {
if (obj.name != ‘’ && data.success == true) {
var partno = obj.up(‘form’).getForm().findField(‘partno’).getValue();
if (obj.name == ‘upload_partimage1’) {
me.form.findField(“temppartimage1”).setValue(partno + ‘_image1.jpg’);
} else if (obj.name == ‘upload_partimage2’) {
me.form.findField(“temppartimage2”).setValue(partno + ‘_image2.jpg’);
} else if (obj.name == ‘upload_partimage3’) {
me.form.findField(“temppartimage3”).setValue(partno + ‘_image3.jpg’);
} else if (obj.name == ‘upload_partimage4’) {
me.form.findField(“temppartimage4”).setValue(partno + ‘_image4.jpg’);
}
}
if (data.success) {
me.msgHide();
}
else {
me.msgError(data.errors);
}
},complete : function(XMLHttpRequest,status){ //请求完成后最终执行参数
if(status==’timeout’){//超时,status还有success,error等值的情况
me.msgError(“超时”);
}
}
});
},
showSize: function (base64url) {//获取base64图片大小,返回字节
var str = base64url.replace(‘data:image/png;base64,’, ‘’);
var equalIndex = str.indexOf(‘=’);
if (str.indexOf(‘=’) > 0) {
str = str.substring(0, equalIndex);
}
var strLength = str.length;
var fileLength = parseInt(strLength - (strLength / 8) 2);
return fileLength;
},
/图片压缩 Beg/
dataURLtoFile: function (dataurl, filename) {//将base64转换为文件
var arr = dataurl.split(‘,’), mime = arr[0].match(/:(.?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while (n—) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, {type: mime});
},
dealImage: function (path, obj, callback) {
/**
图片压缩,默认同比例压缩
path : pc端传入的图
obj : 对象 有 width, height, quality:(0-1) quality 图片缩放比例0-1 越大越清晰
callback : 回调函数有一个参数,base64的字符串数据
/
var img = new Image();
img.src = path;
img.onload = function () {
var that = this;
var ext = path.substring(path.lastIndexOf(“.”) + 1).toLowerCase();
var w = that.width,
h = that.height,
scale = w / h;
w = obj.width || w;
h = obj.height || (w / scale);
var quality = obj.quality;
var canvas = document.createElement(‘canvas’);
var ctx = canvas.getContext(‘2d’);
var anw = document.createAttribute(“width”);
anw.nodeValue = w;
var anh = document.createAttribute(“height”);
anh.nodeValue = h;
canvas.setAttributeNode(anw);
canvas.setAttributeNode(anh);
ctx.drawImage(that, 0, 0, w, h);
if (obj.quality && obj.quality <= 1 && obj.quality > 0) {
quality = obj.quality;
}
var base64 = canvas.toDataURL(‘image/jpeg’, quality);
callback(base64);
}
},