实际项目中运用:


功能:实现上传图片,更改上传图片,移除图片的功能

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
  7. <link rel="stylesheet" href="bootstrap-fileinput.css">
  8. <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
  9. <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  10. <script src="bootstrap-fileinput.js"></script>
  11. </head>
  12. <body>
  13. <div class="form-group">
  14. <div class="col-md-8">
  15. <div class="fileinput fileinput-new" data-provides="fileinput" id="uploadImageDiv">
  16. <div class="fileinput-new thumbnail" style="width: 200px; height: 150px;">
  17. <img src="${companyInfo.image}" alt="" />
  18. </div>
  19. <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;"></div>
  20. <div>
  21. <span class="btn default btn-file"> <span
  22. class="fileinput-new">选择图片</span> <span class="fileinput-exists">更改</span> <input type="file" name="uploadImage" id="uploadImage" /></span>
  23. <a href="#" class="btn default fileinput-exists" data-dismiss="fileinput">移除</a>
  24. <span>请选择1M以内图片</span>
  25. </div>
  26. </div>
  27. <div id="titleImageError" style="color: #a94442"></div>
  28. </div>
  29. </div>
  30. </body>
  31. <script>
  32. var url = '';
  33. $("#uploadImage").fileupload({
  34. url : ROOT + "/security/company/uploadFile",
  35. dataType : 'json',
  36. autoUpload : false,
  37. acceptFileTypes : /(gif|jpe?g|png)$/i,
  38. maxFileSize : 1000000, // 1 MB
  39. imageMaxWidth : 100,
  40. imageMaxHeight : 100,
  41. messages : {
  42. acceptFileTypes : '文件类型不匹配',
  43. maxFileSize : '文件过大',
  44. minFileSize : '文件过小'
  45. }
  46. }).on("fileuploadadd", function(e, data) {
  47. // 当文件添加上去时候调用
  48. $("#titleImageError").html("");
  49. data.submit()
  50. }).on("fileuploaddone", function(e, data) {
  51. // 上传完成时调用
  52. if (data.result.returnState == "ERROR") {
  53. alert("上传失败")
  54. return;
  55. }
  56. url = data.result.returnData.url;
  57. });
  58. function updateMsg() {
  59. $.ajax({
  60. url : ROOT + "/security/company/updateInfo",
  61. data : {
  62. id : $("#companyId").val(),
  63. image : url,
  64. companyName : $("#companyName").val(),
  65. companySite : $("#companySite").val(),
  66. companyLinker : $("#companyLinker").val(),
  67. companyTel : $("#companyTel").val()
  68. },
  69. type : "post",
  70. success : function(data) {
  71. if (data.returnState == "OK") {
  72. layer.msg('操作成功', {
  73. icon : 6,
  74. shade : 0.01,
  75. offset : [ '57px', '1100px' ]
  76. });
  77. $table.bootstrapTable('refresh');
  78. } else {
  79. layer.msg('操作失败', {
  80. icon : 5,
  81. shade : 0.01,
  82. offset : [ '90px', '900px' ]
  83. });
  84. }
  85. }
  86. });
  87. }
  88. </script>
  89. </html>

bootstrap-fileinput.css文件:(github目前正在维护中,之后所有代码上传至我的github)

  1. /*!
  2. * Jasny Bootstrap v3.1.3 (http://jasny.github.io/bootstrap)
  3. * Copyright 2012-2014 Arnold Daniels
  4. * Licensed under Apache-2.0 (https://github.com/jasny/bootstrap/blob/master/LICENSE)
  5. */
  6. .btn-file {
  7. position: relative;
  8. overflow: hidden;
  9. vertical-align: middle;
  10. }
  11. .btn-file > input {
  12. position: absolute;
  13. top: 0;
  14. right: 0;
  15. width: 100%;
  16. height: 100%;
  17. margin: 0;
  18. font-size: 23px;
  19. cursor: pointer;
  20. filter: alpha(opacity=0);
  21. opacity: 0;
  22. direction: ltr;
  23. }
  24. .fileinput {
  25. display: inline-block;
  26. margin-bottom: 9px;
  27. }
  28. .fileinput .form-control {
  29. display: inline-block;
  30. padding-top: 7px;
  31. padding-bottom: 5px;
  32. margin-bottom: 0;
  33. vertical-align: middle;
  34. cursor: text;
  35. }
  36. .fileinput .thumbnail {
  37. display: inline-block;
  38. margin-bottom: 5px;
  39. overflow: hidden;
  40. text-align: center;
  41. vertical-align: middle;
  42. }
  43. .fileinput .thumbnail > img {
  44. max-height: 100%;
  45. }
  46. .fileinput .btn {
  47. vertical-align: middle;
  48. }
  49. .fileinput-exists .fileinput-new,
  50. .fileinput-new .fileinput-exists {
  51. display: none;
  52. }
  53. .fileinput-inline .fileinput-controls {
  54. display: inline;
  55. }
  56. .fileinput-filename {
  57. display: inline-block;
  58. overflow: hidden;
  59. vertical-align: middle;
  60. }
  61. .form-control .fileinput-filename {
  62. vertical-align: bottom;
  63. }
  64. .fileinput.input-group {
  65. display: table;
  66. }
  67. .fileinput.input-group > * {
  68. position: relative;
  69. z-index: 2;
  70. }
  71. .fileinput.input-group > .btn-file {
  72. z-index: 1;
  73. }
  74. .fileinput-new.input-group .btn-file,
  75. .fileinput-new .input-group .btn-file {
  76. border-radius: 0 4px 4px 0;
  77. }
  78. .fileinput-new.input-group .btn-file.btn-xs,
  79. .fileinput-new .input-group .btn-file.btn-xs,
  80. .fileinput-new.input-group .btn-file.btn-sm,
  81. .fileinput-new .input-group .btn-file.btn-sm {
  82. border-radius: 0 3px 3px 0;
  83. }
  84. .fileinput-new.input-group .btn-file.btn-lg,
  85. .fileinput-new .input-group .btn-file.btn-lg {
  86. border-radius: 0 6px 6px 0;
  87. }
  88. .form-group.has-warning .fileinput .fileinput-preview {
  89. color: #8a6d3b;
  90. }
  91. .form-group.has-warning .fileinput .thumbnail {
  92. border-color: #faebcc;
  93. }
  94. .form-group.has-error .fileinput .fileinput-preview {
  95. color: #a94442;
  96. }
  97. .form-group.has-error .fileinput .thumbnail {
  98. border-color: #ebccd1;
  99. }
  100. .form-group.has-success .fileinput .fileinput-preview {
  101. color: #3c763d;
  102. }
  103. .form-group.has-success .fileinput .thumbnail {
  104. border-color: #d6e9c6;
  105. }
  106. .input-group-addon:not(:first-child) {
  107. border-left: 0;
  108. }

bootstrap-fileinput.js:

  1. /* ===========================================================
  2. * Bootstrap: fileinput.js v3.1.3
  3. * http://jasny.github.com/bootstrap/javascript/#fileinput
  4. * ===========================================================
  5. * Copyright 2012-2014 Arnold Daniels
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License")
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ========================================================== */
  19. +function ($) { "use strict";
  20. var isIE = window.navigator.appName == 'Microsoft Internet Explorer'
  21. // FILEUPLOAD PUBLIC CLASS DEFINITION
  22. // =================================
  23. var Fileinput = function (element, options) {
  24. this.$element = $(element)
  25. this.$input = this.$element.find(':file')
  26. if (this.$input.length === 0) return
  27. this.name = this.$input.attr('name') || options.name
  28. this.$hidden = this.$element.find('input[type=hidden][name="' + this.name + '"]')
  29. if (this.$hidden.length === 0) {
  30. this.$hidden = $('<input type="hidden">').insertBefore(this.$input)
  31. }
  32. this.$preview = this.$element.find('.fileinput-preview')
  33. var height = this.$preview.css('height')
  34. if (this.$preview.css('display') !== 'inline' && height !== '0px' && height !== 'none') {
  35. this.$preview.css('line-height', height)
  36. }
  37. this.original = {
  38. exists: this.$element.hasClass('fileinput-exists'),
  39. preview: this.$preview.html(),
  40. hiddenVal: this.$hidden.val()
  41. }
  42. this.listen()
  43. }
  44. Fileinput.prototype.listen = function() {
  45. this.$input.on('change.bs.fileinput', $.proxy(this.change, this))
  46. $(this.$input[0].form).on('reset.bs.fileinput', $.proxy(this.reset, this))
  47. this.$element.find('[data-trigger="fileinput"]').on('click.bs.fileinput', $.proxy(this.trigger, this))
  48. this.$element.find('[data-dismiss="fileinput"]').on('click.bs.fileinput', $.proxy(this.clear, this))
  49. },
  50. Fileinput.prototype.change = function(e) {
  51. var files = e.target.files === undefined ? (e.target && e.target.value ? [{ name: e.target.value.replace(/^.+\\/, '')}] : []) : e.target.files
  52. e.stopPropagation()
  53. if (files.length === 0) {
  54. this.clear()
  55. return
  56. }
  57. this.$hidden.val('')
  58. this.$hidden.attr('name', '')
  59. this.$input.attr('name', this.name)
  60. var file = files[0]
  61. if (this.$preview.length > 0 && (typeof file.type !== "undefined" ? file.type.match(/^image\/(gif|png|jpeg)$/) : file.name.match(/\.(gif|png|jpe?g)$/i)) && typeof FileReader !== "undefined") {
  62. var reader = new FileReader()
  63. var preview = this.$preview
  64. var element = this.$element
  65. reader.onload = function(re) {
  66. var $img = $('<img>')
  67. $img[0].src = re.target.result
  68. files[0].result = re.target.result
  69. element.find('.fileinput-filename').text(file.name)
  70. // if parent has max-height, using `(max-)height: 100%` on child doesn't take padding and border into account
  71. if (preview.css('max-height') != 'none') $img.css('max-height', parseInt(preview.css('max-height'), 10) - parseInt(preview.css('padding-top'), 10) - parseInt(preview.css('padding-bottom'), 10) - parseInt(preview.css('border-top'), 10) - parseInt(preview.css('border-bottom'), 10))
  72. preview.html($img)
  73. element.addClass('fileinput-exists').removeClass('fileinput-new')
  74. element.trigger('change.bs.fileinput', files)
  75. }
  76. reader.readAsDataURL(file)
  77. } else {
  78. this.$element.find('.fileinput-filename').text(file.name)
  79. this.$preview.text(file.name)
  80. this.$element.addClass('fileinput-exists').removeClass('fileinput-new')
  81. this.$element.trigger('change.bs.fileinput')
  82. }
  83. },
  84. Fileinput.prototype.clear = function(e) {
  85. if (e) e.preventDefault()
  86. this.$hidden.val('')
  87. this.$hidden.attr('name', this.name)
  88. this.$input.attr('name', '')
  89. //ie8+ doesn't support changing the value of input with type=file so clone instead
  90. if (isIE) {
  91. var inputClone = this.$input.clone(true);
  92. this.$input.after(inputClone);
  93. this.$input.remove();
  94. this.$input = inputClone;
  95. } else {
  96. this.$input.val('')
  97. }
  98. this.$preview.html('')
  99. this.$element.find('.fileinput-filename').text('')
  100. this.$element.addClass('fileinput-new').removeClass('fileinput-exists')
  101. if (e !== undefined) {
  102. this.$input.trigger('change')
  103. this.$element.trigger('clear.bs.fileinput')
  104. }
  105. },
  106. Fileinput.prototype.reset = function() {
  107. this.clear()
  108. this.$hidden.val(this.original.hiddenVal)
  109. this.$preview.html(this.original.preview)
  110. this.$element.find('.fileinput-filename').text('')
  111. if (this.original.exists) this.$element.addClass('fileinput-exists').removeClass('fileinput-new')
  112. else this.$element.addClass('fileinput-new').removeClass('fileinput-exists')
  113. this.$element.trigger('reset.bs.fileinput')
  114. },
  115. Fileinput.prototype.trigger = function(e) {
  116. this.$input.trigger('click')
  117. e.preventDefault()
  118. }
  119. // FILEUPLOAD PLUGIN DEFINITION
  120. // ===========================
  121. var old = $.fn.fileinput
  122. $.fn.fileinput = function (options) {
  123. return this.each(function () {
  124. var $this = $(this),
  125. data = $this.data('bs.fileinput')
  126. if (!data) $this.data('bs.fileinput', (data = new Fileinput(this, options)))
  127. if (typeof options == 'string') data[options]()
  128. })
  129. }
  130. $.fn.fileinput.Constructor = Fileinput
  131. // FILEINPUT NO CONFLICT
  132. // ====================
  133. $.fn.fileinput.noConflict = function () {
  134. $.fn.fileinput = old
  135. return this
  136. }
  137. // FILEUPLOAD DATA-API
  138. // ==================
  139. $(document).on('click.fileinput.data-api', '[data-provides="fileinput"]', function (e) {
  140. var $this = $(this)
  141. if ($this.data('bs.fileinput')) return
  142. $this.fileinput($this.data())
  143. var $target = $(e.target).closest('[data-dismiss="fileinput"],[data-trigger="fileinput"]');
  144. if ($target.length > 0) {
  145. e.preventDefault()
  146. $target.trigger('click.bs.fileinput')
  147. }
  148. })
  149. }(window.jQuery);

自荐前端干货:

进阶攻略|前端最全的框架总结:https://www.jianshu.com/p/2a8ce7075d79
web开发快速提高工作效率的一些资源:https://www.jianshu.com/p/3cede64e87e5
前端学习的几个网站:https://www.jianshu.com/p/c36463dd56db
老司机程序员用到的各种网站整理:https://www.jianshu.com/p/c806eabe5bec
进阶攻略|前端完整的学习路线:https://www.jianshu.com/p/ed50ee3889d4
八款前端开发人员更轻松的实用在线工具:https://www.jianshu.com/p/267a01fb8bdb
前端几个常用简单的开发手册拿走不谢:https://www.jianshu.com/p/fd9689046a9b
程序员常用的六大技术博客类:https://www.jianshu.com/p/d1614f890282
九款优秀的企业项目协作工具推荐:https://www.jianshu.com/p/7df25e438610
移动端手势的七个事件库:https://www.jianshu.com/p/0754d5daa27e
Bootstrap相关优质项目学习清单:https://www.jianshu.com/p/80d229e7fedc
2018前端越来越流行的的技术:https://www.jianshu.com/p/d4af2aa96cee

原文作者:祈澈姑娘
原文链接:https://www.jianshu.com/u/05f416aefbe1
创作不易,转载请告知

90后前端妹子,爱编程,爱运营,爱折腾。
坚持总结工作中遇到的技术问题,坚持记录工作中所所思所见,欢迎大家一起探讨交流。

文末福利:

福利一:前端,Java,产品经理,微信小程序,Python等10G资源合集大放送:https://www.jianshu.com/p/e8197d4d9880

福利二:微信小程序入门与实战全套详细视频教程。

【领取方法】

关注 【编程微刊】微信公众号:

回复【小程序demo】一键领取130个微信小程序源码demo资源。

回复【领取资源】一键领取前端,Java,产品经理,微信小程序,Python等资源合集10G资源大放送。

bootstrap+fileinput插件实现可预览上传照片功能的更多相关文章

  1. JS代码实用代码实例(输入框监听,点击显示点击其他地方消失,文件本地预览上传)

    前段时间写前端,遇到一些模块非常有用,总结以备后用 一.input框字数监听 <!DOCTYPE html> <html lang="en"> <he ...

  2. php实现头像预览上传功能

    最近在做php第二阶段的项目,需要用到头像上传的功能 我们要完成头像上传功能,一共要写两个php页面,第一个页面我们叫做touxiang.php,第二个页面我们叫做upload.php 1.touxi ...

  3. 对百度WebUploader的二次封装,精简前端代码之图片预览上传(两句代码搞定上传)

    前言 本篇文章上一篇: 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传) 此篇是在上面的基础上扩展出来专门上传图片的控件封装. 首先我们看看效果: 正文 使用方式同 ...

  4. H5实现多图片预览上传,可点击可拖拽控件介绍

    版权声明:欢迎转载,请注明出处:http://blog.csdn.net/weixin_36380516 在做图片上传时发现一个蛮好用的控件,支持多张图片同时上传,可以点击选择图片,也可以将图片拖拽到 ...

  5. 【项目相关】MVC中使用WebUploader进行图片预览上传以及编辑

    项目中需要用到多图片上传功能,于是在百度搜了一下,首先使用了kissy uploader,是由阿里前端工程师们发起创建的一个开源 JS 框架中的一个上传组件...但,后面问题出现了. 在对添加的信息进 ...

  6. jquery.uploadView 实现图片预览上传

    图片上传,网上有好多版本,今天也要做一个查了好多最终找到了一个uploadview 进行了一下修改 来看代码 @{ Layout = null; } <!DOCTYPE html> < ...

  7. js获取base64格式图片预览上传并用php保存到本地服务器指定文件夹

    html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  8. MUI 单个图片上传预览(拍照+系统相册):先选择->预览->上传提交

    1 html部分 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  9. ***Bootstrap FileInput插件的使用经验汇总

    插件下载地址: https://github.com/kartik-v/bootstrap-fileinput/ 官方DEMO查看: http://plugins.krajee.com/file-ba ...

随机推荐

  1. 【Django】序列化

    Django中序列化主要应用于将数据库中检索的数据返回给客户端用户,特别是Ajax请求一般返回为Json格式. * 1.from django.core import serializers** fr ...

  2. syslog日志介绍

    一. syslog简介 syslog是一种工业标准的协议,可用来记录设备的日志.在UNIX系统,路由器.交换机等网络设备中,系统日志(System Log)记录系统中任何时间发生的大小事件.管理者可以 ...

  3. 【Henu ACM Round #12 C】 Alice, Bob, Two Teams

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 考虑任意两个字符串(a,b) 假设a在b的前面 那么如果a+b>=b+a 这里的+表示字符串的链接 那么显然需要交换a,b的位 ...

  4. Windows学习总结(3)——成为电脑高手必备的cmd命令大全

    曾经看电影和电视里面电脑黑客快速敲击电脑键盘,一行行命令在电脑屏幕闪过,一个回车过后,一排排英文象走马灯一样在屏幕上转瞬即逝,那才是我们梦寐以求的高手,有木有!实际上,不光是黑客和系统维护人员,一般的 ...

  5. absolute、relative,toggle()

    測试代码例如以下: <div> <div class="global">不应用样式</div> <div class="glob ...

  6. VPS搭建与IPv6使用教程

    VPS搭建与IPv6使用教程 SoftEther命令: yum -y install gcc zlib-devel openssl-devel readline-devel ncurses-devel ...

  7. 热点共享SS网络

    # 测试系统: Ubuntu 16.04 LTS-lxde-ARM # ***-libev 安装脚本源于 秋水逸冰: https://teddysun.com/358.html # ss-tproxy ...

  8. js39---组合模式,查找遍历树

    /** *有这样一个需求 *有一个学校有2个班(一班,二班) *每个班级分2个小组(一班一组,一班二组,二班一组,二班二组) *学校计算机教室有限,每一个小组分着来上课. *考试的时候大家一起考 *请 ...

  9. PHP和JSON

    PHP和JSON 一.总结 1.php中json的使用方法:php中json的使用超级简单啦,主要是两个函数json_encode(编码)和json_decode(解码),像md5加密 2.json的 ...

  10. Spark MLlib架构解析(含分类算法、回归算法、聚类算法和协同过滤)

    Spark MLlib架构解析 MLlib的底层基础解析 MLlib的算法库分析 分类算法 回归算法 聚类算法 协同过滤 MLlib的实用程序分析 从架构图可以看出MLlib主要包含三个部分: 底层基 ...