Files
sandboxMP/static/js/plugins/jquery.upload.js
2018-10-15 18:54:19 +08:00

64 lines
2.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
jQuery.fn.extend({
uploadPreview: function (opts) {
$(this).each(function(){
var _self = this,
_this = $(this);
opts = jQuery.extend({
Img: "ImgPr",
Width: 100,
Height: 100,
ImgType: ["gif", "jpeg", "jpg", "bmp", "png"],
Callback: function () {}
}, opts || {});
_self.getObjectURL = function (file) {
var url = null;
if (window.createObjectURL != undefined) {
url = window.createObjectURL(file)
} else if (window.URL != undefined) {
url = window.URL.createObjectURL(file)
} else if (window.webkitURL != undefined) {
url = window.webkitURL.createObjectURL(file)
}
return url
};
_this.change(function () {
if (this.value) {
if (!RegExp("\.(" + opts.ImgType.join("|") + ")$", "i").test(this.value.toLowerCase())) {
alert("选择文件错误,图片类型必须是" + opts.ImgType.join("") + "中的一种");
this.value = "";
return false
}
var obj = _this.parent().siblings().children(opts.Img),
imgBox = obj.parent("span"),
imgBoxDom = imgBox[0];
if ($.browser.msie) {
try {
obj.attr('src', _self.getObjectURL(this.files[0]))
} catch (e) {
var src = "";
_self.select();
if (top != self) {
window.parent.document.body.focus()
} else {
_self.blur()
}
src = document.selection.createRange().text;
document.selection.empty();
obj.hide();
imgBox.css({
'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)',
'width': opts.Width + 'px',
'height': opts.Height + 'px'
});
imgBoxDom.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = src
}
} else {
obj.attr('src', _self.getObjectURL(this.files[0]))
}
opts.Callback()
}
});
});
}
});