解決Extjs上傳圖片無法預覽的解決方案

來源:互聯網
上載者:User

複製代碼 代碼如下:
{
width: 450,
fileUpload: true,
fieldLabel: '選擇圖片',
items: [{
xtype: 'textfield',
id: 'up_forth',
name: 'up_forth',
inputType: 'file',
width: 300
}]
}

預覽box
複製代碼 代碼如下:
{
columnWidth: .18,
bodyStyle: ' margin:4px 10px 10px 5px',
layout: 'form',
items: [{
xtype: 'box',
autoEl: {
width: 150, height: 150,
tag: 'div',
id: 'browser_up_forth'
}
}]
}


myfrom表示上傳控制項外圍的FormPanel,, contril_id表示上傳控制項的ID,只要在程式上預覽註冊該方法就可以,preview (myfrom,'up_forth' );
複製代碼 代碼如下:
var preview = function (myform, control_id) {
var img_reg = /\.([jJ][pP][gG]){1}$|\.([jJ][pP][eE][gG]){1}$|\.([gG][iI][fF]){1}$|\.([pP][nN][gG]){1}$|\.([bB][mM][pP]){1}$/
myform.on('render', function (f) {
myform.form.findField(control_id).on('render', function () {
Ext.get(control_id).on('change', function (field, newValue, oldValue) {
var obj = Ext.get(control_id).dom;
var url = getFullPath(obj);
if (img_reg.test(url)) {
var newPreview = Ext.get('browser_' + control_id).dom;
var showPic = Ext.get("showPic_" + control_id);
if (showPic != null) {
showPic.remove();//刪除原來的圖片
}
var imgDiv = document.createElement("div");
imgDiv.id = "showPic_" + control_id;
document.body.appendChild(imgDiv);
imgDiv.style.width = "150px";
imgDiv.style.height = "150px";
imgDiv.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod = scale)";
imgDiv.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = url;
newPreview.appendChild(imgDiv);
}
}, this);
}, this);
}, this);
}

//得到圖片地址
function getFullPath(obj) {
if (obj) {
// ie
if (window.navigator.userAgent.indexOf("MSIE") >= 1) {
obj.select();
return document.selection.createRange().text;
}
// firefox
else if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
if (obj.files) {
return obj.files.item(0).getAsDataURL();
}
return obj.value;
}
return obj.value;
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.