BootStrap文件上传控件

一、准备

1、插件下载地址:https://github.com/kartik-v/bootstrap-fileinput/ 下载后的压缩包解压文件夹内容如下:

  

 

 

        js:插件核心js代码,引用fileinput.min.js/fileinput.js即可,默认插件语言为英文,如需要中文需要引用js/locales/zh.js(其他语言引用相应的js文件)

  js/plugins:相关辅助插件,piexif.min.js(resize image插件)、sortable.js(初始化预览窗口缩略图排序插件)、purify.js(看说明是净化html代码用的,试了下没有搞明白具体有说明作用)

  css:插件核心css代码,fileinput.min.css/fileinput.css(核心css代码,引用任何一个)、fileinput-rtl.min.css/fileinput-rtl.css(选用这组css,文件选择框会和从右向左排列,即选择文件等相关按钮按钮在左边,文件框在右边)

  themes:主题,如需要插件主题样式可引用主题文件夹下的相应js和css文件

  查看示例:

    打开压缩包中的example/index.html或者访问http://plugins.krajee.com/file-basic-usage-demo查看示例。可根据示例的代码选择需要的控件代码使用。

2、代码引用

  需要引用jquery

  需要结合bootstrap使用,即页面需要引入bootstrap相关js和css文件

  引用fileinput.js 和css

  中文需要引用js/locales/zh.js

  需要主题样式时引用themes下相关文件夹中的js和css

  tips:最好以上按顺序引用,免得出现莫名其妙的问题,比如zh.js必须在fileinput.js后引用否则无效

二、使用示例

  1、基本用法,自动将一个普通的input框渲染成高大上的文件上传选择框:

<input id="f_upload" type="file" class="file" />

  只需要将class设置为file即可,效果如下:

 

  2、隐藏文件预览窗口

  

<input id="input-1a" type="file" class="file" data-show-preview="false">

  设置属性:data-show-preview=“false”,等同于初始化属性:showPreview:false

       效果如下:

  3、使用input 属性和data-**属性创建多样化的文件上传框

  

<input id="input-2" name="input2" type="file fileloading" class="file" multiple 
 readonly="false" disabled="false"  data-show-upload="false" data-show-caption="true" data-show-preview="true" data-allowed-file-extensions='["jpg","png"]'>

  属性说明:

    type="file fileloading" file表示此input框将被渲染为文件上传控件,fileloading在选择文件后产生预览前会有友好的转圈的loading提示(文件选择多了后比较容易观察到)

    multiple:标识可以文件多选

    readonly/disabled:这个不用解释了噻

         data-show-upload:等同于属性showUpload,显示上传按钮,同理有data-show-remove、data-show-cancel等详见option

    data-show-caption:显示文件上传的简介

    data-allowed-file-extensions:允许的文件后缀名,另外有data-allowed-file-types详见option

    .........

三、option选项说明:

属性名

属性类型

描述说明

默认值

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: '<i class="glyphicon glyphicon-triangle-left"></i>',

  next: '<i class="glyphicon glyphicon-triangle-right"></i>',

  toggleheader: '<i class="glyphicon glyphicon-resize-vertical"></i>',

  fullscreen: '<i class="glyphicon glyphicon-fullscreen"></i>',

  borderless: '<i class="glyphicon glyphicon-resize-full"></i>',

  close: '<i class="glyphicon glyphicon-remove"></i>'

},

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

 

'<i class="glyphicon glyphicon-file"></i>'

previewFileIconClass

String

 

'file-other-icon'

previewFileIconSettings

Object

 

{}

previewFileExtSettings

Object

 

{}

buttonLabelClass

String

 

'hidden-xs'

browseIcon

String

 

'<i class="glyphicon glyphicon-folder-open"></i>&nbsp;'

browseClass

String

 

'btn btn-primary'

removeIcon

String

 

'<i class="glyphicon glyphicon-trash"></i>'

removeClass

String

 

'btn btn-default'

cancelIcon

String

 

'<i class="glyphicon glyphicon-ban-circle"></i>'

cancelClass

String

 

'btn btn-default'

uploadIcon

String

 

'<i class="glyphicon glyphicon-upload"></i>'

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

 

'<i class="glyphicon glyphicon-exclamation-sign"></i> '

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">&times;</span>'

slugCallback

String

 暂时没有搜到说明,调试显示,在文件选择后会调到这个方法。

null

dropZoneEnabled

boolean

是否显示拖拽区域

true

dropZoneTitleClass

String

拖拽区域类属性设置

'file-drop-zone-title'

fileActionSettings

Object

 

{}

otherActionButtons

String

 

''

textEncoding

String

编码设置

'UTF-8'

ajaxSettings

Object

 

{}

ajaxDeleteSettings

Object

 

{}

showAjaxErrorDetails

boolean

 

true

7、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

 

在预览框中图片已经完全加载完毕后回调的事件

 

8、常见问题与错误

     8.1 引入所需文件后页面刷新查看样式奇怪,浏览器提示错误等,可能是因为js、css文件的引用顺序问题,zh.js需要在fileinput.js后面引入。bootstrap最好在fileinput前引入。

     8.2 文件上传控件在使用$("form").serializeArray()时无法直接读取值,可以通过在文件上传完成后(fileuploaded)给一个隐藏的input框赋值的方式获取form值。

    举例场景:

      form表单中包含文件上传控件,需要在选择文件后不立刻上传,在点击form表单提交按钮后先上传文件,成功后提交表单。

文章来源:https://www.cnblogs.com/parker-yu/p/7207071.html

原文地址:https://www.cnblogs.com/joelan0927/p/10482553.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


前端工程师一般用的是Bootstrap的框架而不是样式,样式一般自己重新定义class类即可,主要样式要随具体情况而定包括位置任何带有class.btn的元素都会继承圆角灰色按钮的默认外观。但是Bootstrap提供了一些选项来定义按钮的样式。<buttontype="button"class="btn">基本
起步导入:<linkrel="stylesheet"href="bootstrap-3.3.7-dist/css/bootstrap.css"><scriptsrc="js/jquery-3.3.1.js"></script><scriptsrc="bootstrap-3.3.7-dist/js/bootstrap.js"></script>屏幕
(1)modal声明一个模态框(2)modal-dialog定义模态框尺寸(3)modal-lg定义大尺寸模态框(4)modal-sm定义小尺寸模态框(5)modal-header(6)modal-body(7)modal-footer<!doctypehtml><html><head><metacharset="utf-8"><title>模态框<itle><linkrel=&quo
图片在Bootstrap版本3中,通过为图片添加 .img-responsive 类可以让图片支持响应式布局。其实质是为图片设置了 max-width:100%;、 height:auto; 和 display:block; 属性,从而让图片在其父元素中更好的缩放。如果需要让使用了 .img-responsive 类的图片水平居
<inputtype="text"class="form-controldatepicker"style="padding:0.375rem0.75rem;"placeholder="开始时间"readonly="true" id="start_time"name="start_time"> $(".datepicke
目录bootstrap-treeview使用小记零、写在前面的话一、功能说明二、特性简述三、实战3.1依赖环境3.2数据源格式3.3Options选项3.4Methods方法3.5Events事件N-2、番外N-1、本文demoN、参考资料bootstrap-treeview使用小记零、写在前面的话p.s.bootst
  一、应用http://www.bootcss.com/进入bootstrap4或bootstrap3中文网,想要快速地将Bootstrap应用到你的项目中,有以下两种办法: 1、bootstrap可以在线引用,方法如下:A、CSS将引入Bootstrap样式表的 <link> 标签复制并粘贴到 <head> 中,并放在所有其他样式表之前。<!
第87节:Java中的Bootstrap基础与SQL入门前言复习什么是JQ?:writelessdomore写更少的代码,做更多的事找出所有兄弟:$("div").siblings()基本过滤器:选择器:过滤器$("div:first"):first:找到第一个元素:last:找到最后一个元素:even:找出偶数索引:odd:找出奇叔索引
1.bootstrap表单(1)form声明一个表单域(2)form-inline内联表单域(3)form-horizontal水平排列表单域(4)form-group表单组、包括表单文字和表单控件(5)form-control文本输入框、下拉列表控件样式(6)checkboxcheck-inline多选框样式(7)radioradio-inline单选框样式(8)input-group表单控件组
<!DOCTYPEhtml><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>首页<itle><
嗯。。。以前做ssh。应该是stratusspringhibernate。 然后现在来了一个新的需求。 要用java,bootstrap,oracle,springboot,jquery,mybatis。 开始,我也挺心虚的,但是后来一看,,,其实本没有必要这么虚。。。毕竟。。。这些东西,写的有问题。。。问题在于没有逻辑。 bootstrap,j
表格基本实例为任意 <table> 标签添加 .table 类可以为其赋予基本的样式—少量的内补(padding)和水平方向的分隔线。这种方式看起来很多余!?但是我们觉得,表格元素使用的很广泛,如果我们为其赋予默认样式可能会影响例如日历和日期选择之类的插件,所以我们选择将此样式独立出来。
1、问题背景   一般情况下,查询列表有查询条件、查询按钮和重置按钮,输入查询条件,点击查询按钮查询列表等数据;点击重置按钮会将查询条件恢复到原始状态 2、实现源码 <!DOCTYPEhtml><html> <head> <metacharset="UTF-8"> <title>Bootstrap-查询按钮和重置按钮<
Bootstrap简介什么是Bootstrap?Bootstrap官网框架:库liblibraryjQuery作为一个框架来讲,提供一套比较便捷的操作DOM的方式把大家都需要的功能预先写好到一些文件这就是一个框架Bootstrap让我们的Web开发更简单,更快捷;注意是Bootstrap不是BootStrap!这是一个词,不是
1.bootstrap图片img-responsive声明响应式图片2.bootstrap字体图标通过字体代替图标,font文件夹需要和css文件夹在同一目录3.bootst导航条(1)navbar声明导航条(2)navbar-default声明默认的导航条样式(3)navbar-inverse声明反白的导航条样式(4)navbar-static-top去掉导航条的圆角(5)n
1.路径导航<!doctypehtml><html><head><metacharset="utf-8"><title>路径导航<itle><linkrel="stylesheet"type="text/css"href="css/bootstrap.min.css"><scripttype="text/ja
问题描述:最近在学习BootStrap,过程中遇到引用glyphicon图标无法显示的问题,经过在百度后该问题已解决。1、首先看一下图标显示失败的页面:2、经过参考大佬们的经验,我找到了解决办法。首先我的BootStrap的css样式表是经过下载之后直接拷贝了其中一个文件到编译器中使用的,没有把所有
BootStrap布局一、BootStrap布局CSS组件主要包括栅格系统、列表组、进度条、icon图标、导航栏等组件。JavaScript插件主要有动画效果、窗体模式、下拉菜单、选项卡等二、网格系统Bootstrap内置了一套响应式、移动优先的流式栅格系统,随着屏幕设备或可视窗口(viewport)尺寸的
1引入所需要的文件2用法
想让bootstrap的table列内容超出部分省略号,要在table上加table-layout:fixed和word-break:break-all,然后在头部thead的th加上宽度百分比,最后在列里加个标签如span,在这个span加上单行超出部分省略号的css:display:inline-block,overflow:hidden,white-space:nowrap,text-overflow:e