bootstrap fileinput 文件上传和回显「建议收藏」

bootstrap fileinput 文件上传和回显「建议收藏」官网下载文件http://plugins.krajee.com/file-input/demobootstrapfileinput是基于bootstrap基础上的上传插件,so必要的bootstrap文件是要引用的…1.引用js和css文件fileinput.cssfileinput.min.js中文字体2.初始化fileinput前台jsp:<…

大家好,又见面了,我是你们的朋友全栈君。

官网下载文件http://plugins.krajee.com/file-input/demo

bootstrap fileinput 是基于bootstrap基础上的上传插件,so必要的bootstrap文件是要引用的…

1.引用js和css文件

fileinput.css

fileinput.min.js

中文字体

2.初始化fileinput

前台

jsp:

<input id="debugId" name="debugId" hidden="true" /> <!-- 文件上传额外参数 -->

<input id="fileInput" name="files" multiple type="file" class="file" value=""/>

如果已经存在,换个名字即可,如class=”file1″class

js:

step1:上传部分,可携带额外参数
$("#fileInput").fileinput('destroy');  // 直接销毁
            	$("#fileInput").fileinput({
                    language: 'zh', //设置语言
                    uploadUrl: GLOBAL.URL+'project/saveFile.do', //上传的地址 http;//localhost:8080/project/saveFile.do
                    showUpload: false, //是否显示上传按钮
                    showCaption: false,//是否显示标题        
                    previewFileIcon: "<i class='glyphicon glyphicon-king'></i>", 
                    maxFileCount: 6, //表示允许同时上传的最大文件个数
                    fileActionSettings:{showUpload: false}, // 控制上传按钮是否显示
                    uploadExtraData: function(previewId, index) {   //额外参数
                        var obj = {};
                        obj.id = fodderType(); // 这里回调方法
                        return obj;
                    },
                    preferIconicPreview: true, // 开启用图标替换预览效果
   previewFileIconSettings: { // configure your icon file extensions
       'doc': '<i class="fa fa-file-word-o text-primary"></i>',
       'xls': '<i class="fa fa-file-excel-o text-success"></i>',
       'ppt': '<i class="fa fa-file-powerpoint-o text-danger"></i>',
       'pdf': '<i class="fa fa-file-pdf-o text-danger"></i>',
       'txt': '<i class="fa fa-file-text-o text-info"></i>',
       'zip': '<i class="fa fa-file-archive-o text-muted"></i>',
       'htm': '<i class="fa fa-file-code-o text-info"></i>',
       'mov': '<i class="fa fa-file-movie-o text-warning"></i>',
       'mp3': '<i class="fa fa-file-audio-o text-warning"></i>'
   },
previewFileExtSettings: {
       'doc': function(ext) {
           return ext.match(/(doc|docx)$/i);
       },
       'xls': function(ext) {
           return ext.match(/(xls|xlsx)$/i);
       },
       'ppt': function(ext) {
           return ext.match(/(ppt|pptx)$/i);
       },
       'zip': function(ext) {
           return ext.match(/(zip|rar|tar|gzip|gz|7z)$/i);
       },
       'htm': function(ext) {
           return ext.match(/(htm|html)$/i);
       },
       'mov': function(ext) {
           return ext.match(/(avi|mpg|mkv|mov|mp4|3gp|webm|wmv)$/i);
       },
       'mp3': function(ext) {
           return ext.match(/(mp3|wav)$/i);
       },
       'txt': function(ext) {
           return ext.match(/(txt|ini|csv|java|php|js|css)$/i);
       }
   },
   layoutTemplates:{ // 预览图片按钮控制,这里屏蔽预览按钮
   	actionZoom:''
   }
});
fodderType = function() {
    return $("#debugId").val();

};

step2:单独调用上传
$("#fileInput").fileinput("upload");

step3:控件回显重新初始化
// 假设后台信息已经查询完毕!假设为data!!

                            $("#fileInput").fileinput('destroy');
            				$("#fileInput").fileinput({
            			    	language: 'zh', //设置语言
            			        uploadUrl: GLOBAL.URL+'project/saveFile.do?id='+bugId, //上传的地址
            			        showUpload: false, //是否显示上传按钮
            			        showCaption: false,//是否显示标题        
            			        maxFileCount: 6, //表示允许同时上传的最大文件个数
            			        fileActionSettings:{showUpload: false},//是否显示上文件上的上传按钮
            					overwriteInitial: false,
            					allowedPreviewTypes: ['image'],
            					/*
                                !这里是预览的数据格式
                                initialPreview: [
            						// IMAGE RAW MARKUP
            						'http://localhost:8080/amp/base/imgs/app-default.jpg',
            						// IMAGE RAW MARKUP
            						'http://localhost:8080/amp/base/imgs/app-default.jpg',
            						// TEXT RAW MARKUP
            						'123asd啊实打实',
            						'http://localhost:8080/amp/base/imgs/123.docx'
            					],*/
            					initialPreview : data.initialPreview,
            					initialPreviewAsData: true, // allows you to set a raw markup
            					initialPreviewFileType: 'image', // image is the default and can be overridden in config below
            					initialPreviewDownloadUrl: GLOBAL.URL+'project/downFile.do?image={key}', 
            					/*
                                !这里是回显的数据格式,后台查询后,按照格式展示即可,url是删除访问地址,key是删除的id
                                initialPreviewConfig: [
            						{type: "image", caption: "Image-1.jpg", size: 847000, url: "/amp/project/delFile.do", key: 1},
            						{type: "image", caption: "Image-2.jpg", size: 817000, url: "/amp/project/delFile.do", key: '1519713821098wwp4h8v'},  // set as raw markup
            						{type: "text", size: 1430, caption: "LoremIpsum.txt", url: "/amp/project/delFile.do", key: 3},
            						{type: "office", size: 102400, caption: "123.docx", url: "/amp/project/delFile.do", key: '1519788281200pwxfx87'}
            					],*/
            					initialPreviewConfig : data.initialPreviewConfig,
            				       preferIconicPreview: true, // this will force thumbnails to display icons for following file extensions
            				    previewFileIconSettings: { // configure your icon file extensions
            				        'doc': '<i class="fa fa-file-word-o text-primary"></i>',
            				        'xls': '<i class="fa fa-file-excel-o text-success"></i>',
            				        'ppt': '<i class="fa fa-file-powerpoint-o text-danger"></i>',
            				        'pdf': '<i class="fa fa-file-pdf-o text-danger"></i>',
            				        'txt': '<i class="fa fa-file-text-o text-info"></i>',
            				        'zip': '<i class="fa fa-file-archive-o text-muted"></i>',
            				        'htm': '<i class="fa fa-file-code-o text-info"></i>',
            				        'mov': '<i class="fa fa-file-movie-o text-warning"></i>',
            				        'mp3': '<i class="fa fa-file-audio-o text-warning"></i>'
            				    },
            					previewFileExtSettings: { // configure the logic for determining icon file extensions
            				        'doc': function(ext) {
            				            return ext.match(/(doc|docx)$/i);
            				        },
            				        'xls': function(ext) {
            				            return ext.match(/(xls|xlsx)$/i);
            				        },
            				        'ppt': function(ext) {
            				            return ext.match(/(ppt|pptx)$/i);
            				        },
            				        'zip': function(ext) {
            				            return ext.match(/(zip|rar|tar|gzip|gz|7z)$/i);
            				        },
            				        'htm': function(ext) {
            				            return ext.match(/(htm|html)$/i);
            				        },
            				        'mov': function(ext) {
            				            return ext.match(/(avi|mpg|mkv|mov|mp4|3gp|webm|wmv)$/i);
            				        },
            				        'mp3': function(ext) {
            				            return ext.match(/(mp3|wav)$/i);
            				        },
            				        'txt': function(ext) {
            				            return ext.match(/(txt|ini|csv|java|php|js|css)$/i);
            				        }
            				    },
            				    layoutTemplates:{
            				    	actionZoom:''
            				    },
            					uploadExtraData: {
            						img_key: "1000",
            						img_keywords: "happy, nature"
            					}
            				});

 

                

后台上传:

@RequestMapping("/saveFile")
	@ResponseBody
	public String saveFile(@RequestParam(value="files") MultipartFile file,
			HttpServletRequest request,HttpServletResponse response){
		OutputStream os = null;
		InputStream is = null;
		boolean returnBoolean = false; 
		String bug_id = request.getParameter("id");   // 传过来的额外参数
		try {
			String path = "E:\\WORK\\TOMCAT\\apache-tomcat-8.5.24-amp\\wtpwebapps\\amp";
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");   
			String currentDate = df.format(System.currentTimeMillis());
			String pathTemp = path + File.separator + currentDate + File.separator + bug_id;
			File fileTemp = new File(pathTemp);
			if(!fileTemp.exists()) {
				fileTemp.mkdirs();
			}
			String fullFilePath = pathTemp + File.separator + file.getOriginalFilename();
			File fullFile = new File(fullFilePath);
			
			os = new FileOutputStream(fullFile);
			is =file.getInputStream();
			byte buf[] = new byte[1024];//可以修改 1024 以提高读取速度
			int length = 0;
			while( (length = is.read(buf)) > 0 ){
				os.write(buf, 0, length);
			}
			returnBoolean = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
            if(is !=null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(os !=null) {
				try {
					os.flush();
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return JSONObject.fromObject("{\"success\":"+ returnBoolean +"}").toString();
	}

 

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

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/134055.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Oracle之删除数据之后如何恢复的方法总结

    Oracle之删除数据之后如何恢复的方法总结导读:1、delete误删除的解决方法;2、drop误删除的解决方法;3、闪回整个数据库;4、总结以下以oracle数据库为例,介绍关于表中数据删除的解决办法。(不考虑全库备份和利用归档日志

    2022年7月1日
    22
  • C语言的字符串分割

    说起来很有意思,自认为对C语言理解得还是比较深刻的。但居然到今天才知道有个strtok函数,试用了一下突然感慨以前做了多少重复劳动。每次需要解析配置文件,每次需要分割字符串,居然都是自己去分割字符串,

    2021年12月24日
    40
  • linux find命令的使用_linux打包命令tar

    linux find命令的使用_linux打包命令tarLinux中find命令是系统中查找文件的命令,可以帮助用户快速找出自己所需要的文件。通过文件名查找find-name文件名find/etc/-namepasswd###查找/etc目录下的passwd通过文件嵌套层数查找find-maxdepth层数###查找文件时最大层数find-mindepth层数###查找文件时最小层数12例;我们查找/etc中所有.conf文件,可以看…

    2022年10月9日
    2
  • 切片

    切片

    2021年9月11日
    51
  • 神经网络学习(十三)卷积神经网络的MATLAB实现

    神经网络学习(十三)卷积神经网络的MATLAB实现系列博客是博主学习神经网络中相关的笔记和一些个人理解,仅为作者记录笔记之用,不免有很多细节不对之处。卷积神经网络回顾上一节,我们简单探讨了卷积神经网络的反向传播算法,本节我们着手实现了一个简单的卷积神经网,在此之前先以最基本的批量随机梯度下降法+L2正则化对对卷积神经网络的反向传播算法做一个很简单回顾。需要确定参数有:小批量数据的大小mmmCNN模型的层数LLL和所有隐…

    2022年9月22日
    4
  • coreseek_coreone

    coreseek_coreoneSQL结构化查询语言(是一种标准,所有的关系型数据库Mysql,sqlserver,oracle)sphinx的使用两种方式:第一种:使用sphinx的API来操作sphinx(常用)sphi

    2022年8月2日
    8

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号