下载地点、API和DOM地点(英语好的小伙伴能够看看)
下载地点:https://github.com/kartik-v/b…
API文档 :http://plugins.krajee.com/fil…
D E M O:http://plugins.krajee.com/fil…
做项目用到bootstrap fileinput插件上传文件,在用的过程当中,碰到一些问题,所以想着整顿一份比较细致的文档,轻易本身以后运用,也愿望能给人人带来协助,若有毛病,愿望人人主动斧正,主动交换。
注重:第三部份内容由于存在i标签,某些笔墨被转换成图标
一、引入文件
<link href=”../css/bootstrap.min.css”rel=”stylesheet”>
<link href=”../css/fileinput.css” media=”all”rel=”stylesheet” type=”text/css” />
<scriptsrc=”../js/jquery-2.0.3.min.js”></script>
<script src=”../js/fileinput.js”type=”text/javascript”></script>
<script src=”../js/bootstrap.min.js”type=”text/javascript”></script>
二、初始化设置
//初始化fileinput
initFileInput();
function initFileInput() {
$("#uploadImg").fileinput({
language: 'zh', //设置言语
dropZoneTitle: '能够将图片拖放到这里 …支撑多文件上传',
uploadUrl: "index.php", //上传的地点
uploadExtraData: function(previewId, index) { //该插件能够向您的服务器要领发送附加数据。这能够经由过程uploadExtraData在键值对中设置为关联数组对象来完成。所以假如你有设置uploadExtraData={id:'kv-1'},在PHP中你能够读取这些数据$_POST['id']
var id = $('#id').val();
return {seriesId: id};
},
allowedFileExtensions: ['jpg','png'],//吸收的文件后缀
uploadAsync: true, //默许异步上传
showUpload: true, //是不是显现上传按钮
showRemove: true, //显现移除按钮
showPreview: true, //是不是显现预览
showCancel:true, //是不是显现文件上传作废按钮。默许为true。只要在AJAX上传过程当中,才会启用和显现
showCaption: true,//是不是显现文件题目,默许为true
browseClass: "btn btn-primary", //文件挑选器/阅读按钮的CSS类。默许为btn btn-primary
dropZoneEnabled: true,//是不是显现拖拽地区
minImageWidth: 50, //图片的最小宽度
minImageHeight: 50,//图片的最小高度
maxImageWidth: 1000,//图片的最大宽度
maxImageHeight: 1000,//图片的最大高度
maxFileSize: 1024,//单元为kb,假如为0示意不限定文件大小
minFileCount: 1, //每次上传许可的起码文件数。假如设置为0,则示意文件数是可选的。默许为0
maxFileCount: 0, //每次上传许可的最大文件数。假如设置为0,则示意许可的文件数是无限定的。默许为0
previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",//当检测到用于预览的不可读文件范例时,将在每一个预览文件缩略图中显现的图标。默许为<i class="glyphicon glyphicon-file"></i>
layoutTemplates:{
actionUpload:''//去除上传预览缩略图中的上传图片
actionZoom:'', //去除上传预览缩略图中的检察概况预览的缩略图标
actionDownload:'' //去除上传预览缩略图中的下载图标
actionDelete:'', //去除上传预览的缩略图中的删除图标
},//对象用于衬着规划的每一个部份的模板设置。您能够设置以下模板来掌握窗口小部件规划.eg:去除上传图标
msgFilesTooMany: "挑选上传的文件数目({n}) 凌驾许可的最大数值{m}!",//字符串,当文件数凌驾设置的最大计数时显现的音讯 maxFileCount。默许为:挑选上传的文件数({n})超越了许可的最大限定{m}。请重试您的上传!
}).on('filebatchpreupload', function(event, data) { //该要领将在上传之前触发
var id = $('#id option:selected').val();
if(id == 0){
return {
message: "请挑选", // 考证毛病信息在上传前要显现。假如设置了这个设置,插件会在调用时自动中断上传,并将其显现为毛病音讯。您能够运用此属性来读取文件并实行本身的自定义考证
data:{} // any other data to send that can be referred in `filecustomerror`
};
}
});
}
//fileuploaded此事宜仅针对ajax上传触发,并在每一个缩略图文件上传完成后触发。此事宜仅针对ajax上传并在以下情况下触发:当点击每一个预览缩略图中的上传图标而且文件上传胜利时,或许当你有 uploadAsync设置为true您已触发批量上传。在这类情况下,fileuploaded每一个人挑选的文件被上传胜利后,触发事宜。
var id_str = '';
$('#uploadImg').on('fileuploaded', function(event, data, previewId, index) {
if(typeof(data.response.id) != 'undefined'){
id_str = id_str+data.response.id+',';
}
});
// filebatchuploadcomplete此事宜仅在ajax上传和完成同步或异步ajax批量上传后触发。
$('#uploadImg').on('filebatchuploadcomplete',function (event,files,extra) {
if(id_str.length == 0){
layer.msg('上传失利', {icon: 0});//弹框提醒
return false;
}
setTimeout(function(){ //实行延时封闭
closeSelf();
},1000);
});
三、Options 申明
属性名 | 属性范例 | 形貌申明 | 默许值 |
---|---|---|---|
language | String | 多言语设置,运用时需提早引入locales文件夹下对应的言语文件,中文zh,引入言语文件必需放在fileinput.js以后 | ‘en’ |
showCaption | Boolean | 是不是显现被选文件的简介 | true |
showBrowse | Boolean | 是不是显现阅读按钮 | true |
showPreview | Boolean | 是不是显现预览地区 | true |
showRemove | Boolean | 是不是显现移除按钮 | true, |
showUpload | Boolean | 是不是显现上传按钮 | true, |
showCancel | Boolean | 是不是显现作废按钮 | true, |
showClose: | Boolean | 是不是显现封闭按钮 | true |
showUploadedThumbs | Boolean | true | |
browseOnZoneClick | Boolean | false | |
autoReplace | Boolean | 是不是自动替代当前图片,设置为true时,再次挑选文件, 会将当前的文件替代掉。 | false |
generateFileId | Object | null | |
previewClass | String | 增加预览按钮的类属性 | ‘’ |
captionClass | String | ‘’ | |
frameClass | String | ‘krajee-default’ | |
mainClass | String | ‘file-caption-main’ | |
mainTemplate | Object | null | |
purifyHtml | Boolean | true | |
fileSizeGetter | Object | null | |
initialCaption | String | ” | |
initialPreview | Array/Object | [] | |
initialPreviewDelimiter | String | ‘$$‘ | |
initialPreviewAsData | Boolean | false | |
initialPreviewFileType | String | ‘image’ | |
initialPreviewConfig | Array/Object | [] | |
initialPreviewThumbTags | Array/Object | [] | |
previewThumbTags | Object | {} | |
initialPreviewShowDelete | Boolean | true | |
removeFromPreviewOnError | Boolean | false | |
deleteUrl | String | 删除图片时的要求途径 | ” |
deleteExtraData | Object | 删除图片时分外传入的参数 | {} |
overwriteInitial | Boolean | true | |
previewZoomButtonIcons | Object | {prev: ”,next: ”,toggleheader: ”,fullscreen: ”,borderless: ”,close: ”}, | |
previewZoomButtonClasses | Object | {prev: ‘btn btn-navigate’,next: ‘btn btn-navigate’,toggleheader: ‘btn btn-default btn-header-toggle’,fullscreen: ‘btn btn-default’,borderless: ‘btn btn-default’,close: ‘btn btn-default’}, | |
preferIconicPreview | Boolrean | false | |
preferIconicZoomPreview | Boolrean | false | |
allowedPreviewTypes | undefined | undefined | |
allowedPreviewMimeTypes | Object | null | |
allowedFileTypes | Object | 吸收的文件后缀,如[‘jpg’, ‘gif’, ‘png’],不填将不限定上传文件后缀范例 | null |
allowedFileExtensions | Object | null | |
defaultPreviewContent | Object | null | |
customLayoutTags | Object | {} | |
customPreviewTags | Object | {} | |
previewFileIcon | String | ” | |
previewFileIconClass | String | ‘file-other-icon’ | |
previewFileIconSettings | Object | {} | |
previewFileExtSettings | Object | {} | |
buttonLabelClass | String | ‘hidden-xs’ | |
browseIcon | String | ‘ ‘ | |
browseClass | String | ‘btn btn-primary’ | |
removeIcon | String | ” | |
removeClass | String | ‘btn btn-default’ | |
cancelIcon | String | ” | |
cancelClass | String | ‘btn btn-default’ | |
uploadIcon | String | ” | |
uploadClass | String | ‘btn btn-default’ | |
uploadUrl | String | 上传文件途径 | null |
uploadAsync | boolean | 是不是为异步上传 | true |
uploadExtraData | 上传文件时分外通报的参数设置 | {} | |
zoomModalHeight | number | 480 | |
minImageWidth | String | 图片的最小宽度 | null |
minImageHeight | String | 图片的最小高度 | null |
maxImageWidth | String | 图片的最大宽度 | null |
maxImageHeight | String | 图片的最大高度 | null |
resizeImage | boolean | false | |
resizePreference | String | ‘width’ | |
resizeQuality | number | 0.92 | |
resizeDefaultImageType | String | ‘image/jpeg’ | |
minFileSize | number | 单元为kb,上传文件的最小大小值 | 0 |
maxFileSize | number | 单元为kb,假如为0示意不限定文件大小 | 0 |
resizeDefaultImageType | number | 25600(25MB) | |
minFileCount | number | 示意同时最小上传的文件个数 | 0 |
maxFileCount | number | 示意许可同时上传的最大文件个数 | 0 |
validateInitialCount | boolean | false | |
msgValidationErrorClass | String | ‘text-danger’ | |
msgValidationErrorIcon | String | ‘ ‘ | |
msgErrorClass | String | ‘file-error-message’ | |
progressThumbClass | String | “progress-bar progress-bar-success progress-bar-striped active” | |
rogressClass | String | “progress-bar progress-bar-success progress-bar-striped active” | |
progressCompleteClass | String | “progress-bar progress-bar-success” | |
progressErrorClass | String | “progress-bar progress-bar-danger” | |
progressUploadThreshold | number | 99 | |
previewFileType | String | 预览文件范例,内置[‘image’, ‘html’, ‘text’, ‘video’, ‘audio’, ‘flash’, ‘object’,‘other‘]等花样 | ‘image’ |
elCaptionContainer | String | null | |
elCaptionText | String | 设置题目栏提醒信息 | null |
elPreviewContainer | String | null | |
elPreviewImage | String | null | |
elPreviewStatus | String | null | |
elErrorContainer | String | null | |
errorCloseButton | String | ‘<span class=”close kv-error-close”>×</span>’ | |
slugCallback | function | 挑选后未上传前 回调要领 | null |
dropZoneEnabled | boolean | 是不是显现拖拽地区 | true |
dropZoneTitleClass | String | 拖拽地区类属性设置 | ‘file-drop-zone-title’ |
fileActionSettings | Object | 设置预览图片的显现款式 | {showRemove: true,showUpload: false,showZoom: true,showDrag: true,removeIcon: ”,removeClass: ‘btn btn-xs btn-default’,removeTitle: ‘Remove file’,uploadIcon: ”,uploadClass: ‘btn btn-xs btn-default’,uploadTitle: ‘Upload file’,zoomIcon: ”,zoomClass: ‘btn btn-xs btn-default’,zoomTitle: ‘View Details’,dragIcon: ”,dragClass: ‘text-info’,dragTitle: ‘Move / Rearrange’,dragSettings: {},indicatorNew: ”,indicatorSuccess: ”,indicatorError: ”,indicatorLoading: ”,indicatorNewTitle: ‘Not uploaded yet’,indicatorSuccessTitle: ‘Uploaded’,indicatorErrorTitle: ‘Upload Error’,indicatorLoadingTitle: ‘Uploading …’} |
otherActionButtons | String | ” | |
textEncoding | String | 编码设置 | ‘UTF-8’ |
ajaxSettings | Object | {} | |
ajaxDeleteSettings | Object | {} | |
showAjaxErrorDetails | boolean | true |
四、提醒申明设置
属性名 | 默许值 | 中文 |
---|---|---|
fileSingle | file | 文件 |
filePlural | files | 个文件 |
browseLabel | Browse &hellip | 挑选 … |
removeLabel | Remove | 移除 |
removeTitle | Clear selected files | 消灭选中文件 |
cancelLabel | Cancel | 作废 |
cancelTitle | Abort ongoing upload | 作废进行中的上传 |
uploadLabel | Upload | 上传 |
uploadTitle | Upload selected files | 上传选中文件 |
msgNo | No | 没有 |
msgNoFilesSelected | No files selected | “” |
msgCancelled | Cancelled | 作废 |
msgZoomModalHeading | Detailed Preview | 细致预览 |
msgSizeTooSmall | File “{name}” ({size} KB) is too small and must be larger than {minSize} KB. | File “{name}” ({size} KB) is too small and must be larger than {minSize} KB. |
msgSizeTooLarge | File “{name}” ({size} KB) exceeds maximum allowed upload size of {maxSize} KB. | 文件 “{name}” ({size} KB) 凌驾了许可大小 {maxSize} KB. |
msgFilesTooLess | You must select at least {n} {files} to upload. | 你必需挑选起码 {n} {files} 来上传. |
msgFilesTooMany | Number of files selected for upload ({n}) exceeds maximum allowed limit of {m}. | 挑选的上传文件个数 ({n}) 超越最大文件的限定个数 {m}. |
msgFileNotFound | File “{name}” not found! | 文件 “{name}” 未找到! |
msgFileSecured | Security restrictions prevent reading the file “{name}”. | 平安限定,为了防备读取文件 “{name}”. |
msgFileNotReadable | File “{name}” is not readable. | 文件 “{name}” 不可读. |
msgFilePreviewAborted | File preview aborted for “{name}”. | 作废 “{name}” 的预览. |
msgFilePreviewError | An error occurred while reading the file “{name}”. | 读取 “{name}” 时涌现了一个毛病. |
msgInvalidFileName | Invalid or unsupported characters in file name “{name}”. | Invalid or unsupported characters in file name “{name}”. |
msgInvalidFileType | Invalid type for file “{name}”. Only “{types}” files are supported. | 不正确的范例 “{name}”. 只支撑 “{types}” 范例的文件. |
msgInvalidFileExtension | Invalid extension for file “{name}”. Only “{extensions}” files are supported. | 不正确的文件扩展名 “{name}”. 只支撑 “{extensions}” 的文件扩展名. |
msgFileTypes | {‘image’: ‘image’,’html’: ‘HTML’,’text’: ‘text’,’video’: ‘video’,’audio’: ‘audio’,’flash’: ‘flash’,’pdf’: ‘PDF’,’object’: ‘object’}, | {‘image’: ‘image’,’html’: ‘HTML’,’text’: ‘text’,’video’: ‘video’,’audio’: ‘audio’,’flash’: ‘flash’,’pdf’: ‘PDF’,’object’: ‘object’}, |
msgUploadAborted | The file upload was aborted | 该文件上传被中断 |
msgUploadThreshold | Processing… | Processing… |
msgUploadBegin | Initializing… | Initializing… |
msgUploadEnd | Done | Done |
msgUploadEmpty | No valid data available for upload. | No valid data available for upload. |
msgValidationError | Validation Error | 考证毛病 |
msgLoading | Loading file {index} of {files} … | 加载第 {index} 文件 共 {files} … |
msgProgress | Loading file {index} of {files} – {name} – {percent}% completed. | 加载第 {index} 文件 共 {files} – {name} – {percent}% 完成. |
msgSelected | {n} {files} selected | {n} {files} 选中 |
msgFoldersNotAllowed | Drag & drop files only! {n} folder(s) dropped were skipped. | 只支撑拖拽文件! 跳过 {n} 拖拽的文件夹. |
msgImageWidthSmall | Width of image file “{name}” must be at least {size} px. | 宽度的图象文件的”{name}”的必需是最少{size}像素. |
msgImageHeightSmall | Height of image file “{name}” must be at least {size} px. | 图象文件的”{name}”的高度必需最少为{size}像素. |
msgImageWidthLarge | Width of image file “{name}” cannot exceed {size} px. | 宽度的图象文件”{name}”不能凌驾{size}像素. |
msgImageHeightLarge | Height of image file “{name}” cannot exceed {size} px. | 图象文件”{name}”的高度不能凌驾{size}像素. |
msgImageResizeError | Could not get the image dimensions to resize. | 没法猎取的图象尺寸调解。 |
msgImageResizeException | Error while resizing the image.<pre>{errors}</pre> | 毛病而调解图象大小。<pre>{errors}</pre> |
msgAjaxError | Something went wrong with the {operation} operation. Please try again later! | Something went wrong with the {operation} operation. Please try again later! |
msgAjaxProgressError | {operation} failed | {operation} failed |
ajaxOperations | {deleteThumb: ‘file delete’, uploadThumb: ‘file upload’, uploadBatch: ‘batch file upload’, uploadExtra: ‘form data upload’ }, | {deleteThumb: ‘file delete’,uploadThumb: ‘file upload’, uploadBatch: ‘batch file upload’,uploadExtra: ‘form data upload’}, |
dropZoneTitle | Drag & drop files here … | 拖拽文件到这里 … 支撑多文件同时上传 |
dropZoneClickTitle | (or click to select {files}) | (或点击{files}按钮挑选文件) |
previewZoomButtonTitles | {prev: ‘View previous file’,next: ‘View next file’, toggleheader: ‘Toggle header’,fullscreen: ‘Toggle full screen’, borderless: ‘Toggle borderless mode’, close: ‘Close detailed preview’ } | { prev: ‘预览上一个文件’,next: ‘预览下一个文件’,toggleheader: ‘缩放’, fullscreen: ‘全屏’, borderless: ‘无边境形式’,close: ‘封闭当前预览’} |
fileActionSettings | { removeTitle: ‘删除文件’,uploadTitle: ‘上传文件’,zoomTitle: ‘检察概况’,dragTitle: ‘挪动 / 重置’,indicatorNewTitle: ‘没有上传’, indicatorSuccessTitle: ‘上传’,indicatorErrorTitle: ‘上传毛病’, indicatorLoadingTitle: ‘上传 …’}, |
五、Method申明
要领名 | 形貌 |
---|---|
fileerror | 异步上传毛病效果处置惩罚$(‘#uploadfile’).on(‘fileerror’, function(event, data, msg) {}); |
fileuploaded | 异步上传胜利效果处置惩罚$(“#uploadfile”).on(“fileuploaded”, function (event, data, previewId, index) {}) |
filebatchuploaderror | 批量上传毛病效果处置惩罚$(‘#uploadfile’).on(‘filebatchuploaderror’, function(event, data, msg) {}); |
filebatchuploadsuccess | 批量上传胜利效果处置惩罚$(‘#uploadfile’).on(‘filepreupload’, function(event, data, previewId, index) {}); |
filebatchselected | 挑选文件后处置惩罚事宜$(“#fileinput”).on(“filebatchselected”, function(event, files) {}); |
upload | 文件上传要领$(“#fileinput”).fileinput(“upload”); |
fileuploaded | 上传胜利后处置惩罚要领$(“#fileinput”).on(“fileuploaded”, function(event, data, previewId, index) {}); |
filereset | |
fileclear | 点击阅读框右上角X 清空文件前相应事宜$(“#fileinput”).on(“fileclear”,function(event, data, msg){}); |
filecleared | 点击阅读框右上角X 清空文件后相应事宜$(“#fileinput”).on(“filecleared”,function(event, data, msg){}); |
fileimageuploaded | 在预览框中图片已完整加载终了后回调的事宜 |