$(function () {
//初始化upload上传控件
$.fn.initupload = function (p) {
//创建上传按钮
var sd = '';
if ($(this).attr("data-column-type") === "5") {
sd += '
选择文件
';
sd += '';
}
else {
sd += '选择图片
';
sd += '';
}
$(this).html(sd)
.attr("handler", p.handler)
.attr("thumbnailmode", p.thumbnailmode)
.attr("thumbnailwidth", p.thumbnailwidth)
.attr("thumbnailheight", p.thumbnailheight)
.attr("filetype", p.filetype)
.attr("filesize", p.filesize)
.attr("filetypes", p.filetypes).find(".upload-html5-btn").click(function () {
$("#txt" + $(this).attr("data-column-columns") + "_filedata").click();
$("form").attr("data-column-columns", $(this).attr("data-column-columns"));
});
}
});
function filesselected() {
var obj = $("#txt" + $("form").attr("data-column-columns") + "_filedata").parents(".single-upload");
var file = document.getelementbyid('txt' + $("form").attr("data-column-columns") + '_filedata').files[0];
if (file) {
// 如果没有文件类型,则通过后缀名判断(解决微信及360浏览器无法获取图片类型问题)
var type = file.name.match(/\.([^\.]+)$/i)[1];
if (obj.attr("filetypes").indexof("*." + type) === -1) {
if (parent.layer)
parent.layer.alert('选择的文件类型不合要求,请选择以下类型:' + obj.attr("filetypes"));
else
alert('选择的文件类型不合要求,请选择以下类型:' + obj.attr("filetypes"));
return;
}
if (file.size > obj.attr("filesize") * 1024) {
if (parent.layer)
parent.layer.alert('选择文件大于' + math.round((obj.attr("filesize") * 1) / 1024) + 'm,请重新选择');
else
alert('选择文件大于' + math.round((obj.attr("filesize") * 1) / 1024) + 'm,请重新选择');
return;
}
createprogress(obj);
var sendurl = "handler=" + obj.attr("handler");
if (obj.attr("thumbnailmode")) {
sendurl += "&thumbnailmode=" + obj.attr("thumbnailmode") + "&thumbnailwidth=" + obj.attr("thumbnailwidth") + "&thumbnailheight=" + obj.attr("thumbnailheight");
}
var fd = new formdata();
fd.append("filedata", file);
var xhr = new xmlhttprequest();
xhr.upload.addeventlistener("progress", upprogress, false);
xhr.addeventlistener("load", upcomplete, false);
xhr.addeventlistener("error", upfailed, false);
xhr.addeventlistener("abort", upcanceled, false);
xhr.open("post", "/21it/dataaction/fileupload.aspx?" + sendurl);
xhr.send(fd);
}
}
function upprogress(evt) {
if (evt.lengthcomputable) {
var percentcomplete = math.round(evt.loaded * 100 / evt.total);
var progressobj = $("#txt" + $("form").attr("data-column-columns") + "_filedata").parents(".single-upload").children(".upload-progress");
progressobj.find(".bar b").width(percentcomplete + "%");
}
}
//创建上传进度条
function createprogress(targetobj) {
//判断显示进度的div是否存在,不存在则创建
var fileprogressobj = $('').appendto(targetobj);
var progresstext = $('正在上传,请稍候...').appendto(fileprogressobj);
var progressbar = $('').appendto(fileprogressobj);
}
function upcomplete(evt) {
/* this event is raised when the server send back a response */
var obj = $("#txt" + $("form").attr("data-column-columns") + "_filedata").parents(".single-upload");
obj.children(".upload-progress").children(".txt").html("文件上传成功");
obj.children(".upload-progress").remove();
var file = document.getelementbyid('txt' + $("form").attr("data-column-columns") + '_filedata');
file.outerhtml = file.outerhtml;
//alert(evt.target.responsetext);
var jsonstr = eval('(' + evt.target.responsetext + ')');
obj.siblings(".upload-path").val(jsonstr.path);
if (jsonstr.filetype == 0) {
obj.siblings(".upload-file").html(' ');
}
else {
obj.siblings(".upload-file").html('' + jsonstr.thumb.substr(29) + '');
}
}
function upfailed(evt) {
var jsonstr = eval('(' + evt.target.responsetext + ')');
if (parent.layer)
parent.layer.alert("上传文件出错:" + jsonstr.error);
else
alert("上传文件出错:" + jsonstr.error);
}
function upcanceled(evt) {
if (parent.layer)
parent.layer.alert("上传文件被取消");
else
alert("上传文件被取消");
}
//删除上传图片
function deleteuploadimg(obj) {
$(obj).parent().siblings(".upload-path").val("");
$(obj).parent().siblings(".upload-name").val("");
$(obj).parent().siblings(".upload-size").val("");
$(obj).parent().html("");
}