You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DSWeb/Areas/SoftMng/Content/common-js/fileinput.js

189 lines
9.6 KiB
JavaScript

2 years ago
var c_preview = '<div class ="file-preview-frame krajee-default file-preview-initial file-sortable kv-preview-thumb" id="{previewId}" data-fileindex ="{fileindex}" data-template ="{template}" style="margin:2px 5px;padding:2px;">{footer}</div>';
var img_preview = '<div class="file-preview-frame" id="{previewId}" data-fileindex="{fileindex}" data-template="{template}">\n' +
' <div class="kv-file-content">' +
'{data}\n' +
' </div>\n' +
' {footer}\n' +
'</div>\n'
// 通用附件初始化
var FileInput = {
Initial: function(id, hName, btnShow, data) {
$("#" + id).fileinput({
language: 'zh', //语言
uploadAsync: false, //批量是否异步
uploadUrl: "/SoftMng/FileInputHandler/Upload", //上传的地址
deleteUrl: "/SoftMng/FileInputHandler/Delete", //删除的地址
//uploadExtraData: { inputname: fName }, //上传额外参数
//deleteExtraData: { filepath: '' }, //删除额外参数
allowedFileExtensions: [
'jpg', 'gif', 'png', 'bmp', 'jpeg', 'xlsx', 'xls', 'doc', 'docx', 'ppt', 'pptx', 'zip', '7z', 'rar',
'pdf', 'txt', 'iso'
],
autoReplace: false,
showUpload: false, //是否显示上传按钮
showRemove: btnShow ? (data == null ? true : false) : false, //显示移除按钮
showPreview: true, //是否显示预览
showCaption: true, //是否显示被选文件的简介
showBrowse: btnShow, //是否显示浏览按钮
showClose: false,
dropZoneEnabled: false,
maxFileSize: 7189, //单位为kb如果为0表示不限制文件大小 (当前限制大小7M
maxFileCount: 4, //表示允许同时上传的最大文件个数
enctype: 'multipart/form-data',
removeClass: "btn btn-danger",
removeIcon: '<i class="icon-remove-circle"></i>',
zoomClass: "btn btn-primary",
browseClass: 'btn btn-primary',
browseIcon: '<i class="icon-folder-open"></i>',
removeLabel: "",
cancelLabel: "",
browseLabel: "",
removeTitle: "全部删除",
validateInitialCount: true, //验证中是否包含初始预览文件数
overwriteInitial: false, //是否要覆盖最初的预览内容和标题设置
initialPreviewCount: data == null ? 0 : data.initCount,
initialPreview: data == null ? [] : data.initData, //初始化预览
initialPreviewConfig: data == null ? [] : data.initConfig,
initialPreviewAsData: true,
initialPreviewShowDelete: true,
previewSettings: {
image: { width: "0", height: "0" },
html: { width: "0", height: "0" },
text: { width: "0", height: "0" },
office: { width: "0", height: "0" },
gdocs: { width: "0", height: "0" },
video: { width: "0", height: "0" },
audio: { width: "0", height: "0" },
flash: { width: "0", height: "0" },
object: { width: "0", height: "0" },
pdf: { width: "0", height: "0" },
other: { width: "0", height: "0" }
},
previewTemplates: {
generic: c_preview,
html: c_preview,
image: c_preview,
text: c_preview,
office: c_preview,
gdocs: c_preview,
video: c_preview,
audio: c_preview,
flash: c_preview,
object: c_preview,
pdf: c_preview,
other: c_preview
},
layoutTemplates: //更改缩略图页脚模板
{
footer: '<div class="file-thumbnail-footer" style="height:auto">\n' +
'<div class="file-footer-caption" title="{caption}" style="width:70%;float:left;margin-bottom:0;">\n' +
'<div class="file-caption-info">{caption}</div>' +
'</div>\n' +
'<div style="width:30%;float:right">{actions}</div>' +
'<div class="preview-image"><img src="{data}" /></div>'+
'</div>'
},
fileActionSettings: {
showRemove: btnShow,
showUpload: false,
showZoom: false,
showDrag: false,
removeIcon: '<i class="icon-remove-sign"></i>',
removeClass: 'btn btn-xs btn-default',
removeTitle: '移除',
zoomIcon: '<i class="icon-info-sign"></i>',
zoomClass: 'btn btn-xs btn-default',
zoomTitle: '详情',
indicatorNew: '<i class="glyphicon glyphicon-hand-down text-warning"></i>',
indicatorSuccess: '<i class="glyphicon glyphicon-ok-sign text-success"></i>',
indicatorError: '<i class="glyphicon glyphicon-exclamation-sign text-danger"></i>',
indicatorLoading: '<i class="glyphicon glyphicon-hand-up text-muted"></i>',
indicatorNewTitle: 'Not uploaded yet',
indicatorSuccessTitle: 'Uploaded',
indicatorErrorTitle: 'Upload Error',
indicatorLoadingTitle: 'Uploading ...'
}, //设置预览图片的显示样式
previewZoomButtonClasses: {
prev: 'icon-arrow-left',
next: 'icon-arrow-right',
toggleheader: 'icon-exchange',
fullscreen: 'icon-fullscreen',
borderless: 'icon-lemon',
close: ' icon-remove'
},
previewFileIcon: "<iclass='icon-paper-clip'></i>",
msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}"
})
.on('fileremoved', //缩略图删除按钮删除缩略图后触发
function(event, id, index) {
var $hideObj = $('[name ="' + hName + '"]');
try {
var fileobjArray = eval($hideObj.val());
for (var i = 0; i < fileobjArray.length; i++)
if (fileobjArray[i]["id"] === id) fileobjArray.splice(i, 1); //删除对象
$hideObj.val(fileobjArray.length > 0 ? JSON.stringify(fileobjArray) : "");
} catch (e) {
}
})
.on('filedeleted',
function(event, key) {
var $hideObj = $('[name ="' + hName + '"]');
try {
var fileobjArray = eval($hideObj.val());
for (var i = 0; i < fileobjArray.length; i++)
if (fileobjArray[i]["id"] === key) {
FileInput.DeleteArray.push(fileobjArray.splice(i, 1)[0]);//将删除的项目加入待彻底删除数组
} //删除对象
$hideObj.val(fileobjArray.length > 0 ? JSON.stringify(fileobjArray) : "");
} catch (e) {
}
})
.on('filecleared', //清楚按钮事件触发
function(event) {
var $hideObj = $('[name ="' + hName + '"]');
$hideObj.val("");
});
},
Uploaded: function(id, hName, fun) {
$("#" + id).on("filebatchuploadsuccess", //同步上传成功结果处理
function(event, data) {
var result = data.response;
var files = data.files;
data = result.data;
if (result.success) {
var $hideObj = $('[name ="' + hName + '"]');
var fileobjArray = new Array();
try {
if ($hideObj.val() !== "") fileobjArray = eval($hideObj.val());
} catch (e) {
$hideObj.val("");
fileobjArray = new Array();
}
for (var i = 0; i < data.length; i++) {
var obj = new Object();
obj.id = files[i].lastModified;
obj.url = data[i];
obj.size = files[i].size;
fileobjArray.push(obj);
}
$hideObj.val(fileobjArray.length > 0 ? JSON.stringify(fileobjArray) : "");
if (typeof (fun) == "function") fun();
} else
layer.msg(data.msg);
});
//删除(异步执行)
CommonAjax.Post("/SoftMng/FileInputHandler/Delete",
{ filepath: JSON.stringify(FileInput.DeleteArray) },
function (res) {
if (!res.success) console.log(res.msg);
});
FileInput.DeleteArray = new Array();//初始化删除数组
//上传(异步执行)
var fileStack = $("#" + id).fileinput('getFileStack');//获取当前要上传的文件
if (fileStack.length > 0)
$("#" + id).fileinput('upload');
else if (typeof (fun) == "function") fun();
},
DeleteArray: new Array()
}