图片回显和选择图片预览:
点击图片展示:
以下功能亲测,部分样式和js根据情况自行调整
<%--
Created by IntelliJ IDEA.
User: yutyi
Date: 2018/9/3
Time: 22:44
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>${title}</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<!--引入jquery,bootstrap的js/css样式,msgbox是自定义js,文中引用到可以删除-->
<%@include file="/common/meta.jsp"%>
</head>
<body class="hold-transition skin-blue">
<div>
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="box" style="margin-bottom: 80px;">
<form id="myform" class="form-horizontal" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="col-xs-2 control-label">附件</label>
<div class="col-xs-10">
<label style="border: 1px dashed #dbdadd;padding: 10px;background-color: #80808033;">
<input style="position:absolute;opacity: 0;width: 35px;height: 35px;left: 5px;" id="choose-file" multiple type="file" name="file" accept="image/gif,image/jpeg,image/x-png,image/png"/>
<div style="width: 35px;height: 35px;background: url('${contextPath}/static/img/upload/icon_upload.png');background-size: 35px 35px;"></div>
</label>
<!--图片预览列表-->
<div class="file-list" style="display: inline-block;width: auto;position: relative;top: -30px;">
</div>
</div>
</div>
<!-- 模态框(Modal),点击图片放大 -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<img id="imgSrc" src="" alt="" style="width: 570px;height: 570px;"/>
</div>
</div>
</div>
</div>
</div>
<div class="dialog-footer">
<button type="button" class="btn btn-primary" onclick="doSave();">保存</button>
<button type="button" class="btn btn-default" onclick="doClose();">关闭</button>
</div>
</form>
</div>
</div>
</div>
</section>
</div>
<script type="text/javascript">
/*
* 保存
*/
function doSave(){
var valid = $('#myform').valid();
if (valid) {
//使用formData发送ajax请求,注意[0],发现很多博客都没有写
var formData = new FormData($("#myform")[0]);
//删除formData中的File对象,设置将我们需要上传的fileList中File对象
formData.delete("file");
for (var i=0;i<fileList.length;i++) {
formData.append("file",fileList[i]);
}
//添加需要删除的已上传的图片的id,后台解析删除
formData.append("removeList",removeList);
$.ajax({
url : "${contextPath}/upload/multi",
data : formData,
type : "post",
dataType : "json",
cache : false,//上传文件无需缓存
processData : false,//用于对data参数进行序列化处理 这里必须false
contentType : false, //必须
success : function (resp) {
if(resp.success){
alert("保存成功");
}else{
alert("保存失败");
}
}
});
}
}
$(function () {
/**
* 编辑页面,获取已上传的图片,根据自己的需求更改
* */
var deviceId = "${param.deviceId}";
if (deviceId) {
var url = "${contextPath}/device/img";
$.post(url,{
deviceId:deviceId},function (data) {
for (var i=0;i<data.length;i++) {
//attachmentId是已上传图片的id,一般图片存储在服务器端规定的某个文件夹,可以使用nginx代理来回显
$list.append('<label data-id="'+data[i].attachmentId+'"><img src=localhost:81'+data[i].url+' alt="" style="width: 55px;height: 55px;" onclick="showImage(this)"/><i class="file-del" style="position: relative;left:-15px;top: -20px;color: red;">×</i></label>')
}
});
}
});
/*
* 图片上传
* */
//选择文件按钮
$file = $("#choose-file");
//回显的列表
$list = $('.file-list');
//选择要上传的所有文件
fileList = [];
//已添加需要删除的文件
removeList = "";
//当前选择上传的文件
var curFile;
// 选择按钮change事件,实例化fileReader,调它的readAsDataURL并把原生File对象传给它,监听它的onload事件,load完读取的结果就在它的result属性里了。它是一个base64格式的,可直接赋值给一个img的src
$file.on('change',function(){
//原生的文件对象(File对象数组),相当于$file.get(0).files[0];
curFile = this.files;
//将curFile数组和FileList数组合并
fileList = fileList.concat(Array.from(curFile));
console.log(fileList);
for(var i=0,len = curFile.length;i < len;i++){
reviewFile(curFile[i])
}
});
//回显图片
function reviewFile(file){
//实例化fileReader,
var fd = new FileReader();
//获取当前选择文件的类型
var fileType = file.type;
//调它的readAsDataURL并把原生File对象传给它,
fd.readAsDataURL(file);//base64
//监听它的onload事件,load完读取的结果就在它的result属性里了
fd.onload = function(){
$list.append('<label class="file-item"><img src="'+this.result+'" alt="" style="width: 55px;height: 55px;" onclick="showImage(this)"/><i class="file-del" style="position: relative;left:-15px;top: -20px;color: red;">×</i></label>')
}
}
//删除图片
$(".file-list").on('click','.file-del',function(){
var $parent = $(this).parent();
var index = $parent.index(".file-item");
//index==-1时表示是已上传回显的图片
if (index == -1) {
removeList += $parent.attr("data-id")+",";
}
console.log(removeList);
//删除选择的图片
fileList.splice(index,1);
$parent.remove();
});
//点击图片显示放大图(使用bootstrap的模态框)
function showImage(e) {
$("#imgSrc").attr("src",e.src);
$('#myModal').modal('show');
}
</script>
</body>
</html>
上传多图片的后端控制类,仅供参考
package zyun.bme.platform.device.web;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import zyun.bme.platform.common.ConfigProperties;
import zyun.bme.platform.common.Constants;
import zyun.bme.platform.common.U;
import zyun.bme.platform.device.entity.Attachment;
import zyun.bme.platform.device.service.AttachmentService;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* 多文件上传控制器
*
* @author yutyi
* @date 2018/09/27
*/
@Controller
@RequestMapping("/upload")
public class MultiFileUploadController {
private Logger logger = LoggerFactory.getLogger(MultiFileUploadController.class);
@Autowired
private AttachmentService attachmentService;
/**
* 文件上传(支持多个文件),单个文件时也可以使用MultipartFile作为方法参数
* @param request
* @return
* @throws IOException
*/
@RequestMapping(value = {
"/multi"})
public String multiFileUpload(MultipartHttpServletRequest request,@RequestParam(required = false)String removeList) throws IOException {
//删除需要删除的图片,仅通过图片id删除表中数据
if (StringUtils.isNotEmpty(removeList)) {
String[] images = removeList.split(",");
ArrayList<Long> list = new ArrayList<>();
for (String image : images) {
if (StringUtils.isNotEmpty(image)) {
list.add(Long.parseLong(image));
}
}
if (list.size() > 0) {
attachmentService.deleteImage(list);
}
}
//获取所有的文件上传表单内容
List<MultipartFile> files = request.getFiles("file");
if (files == null || files.size() == 0 ) {
return "forward:/device/save";
}
for (MultipartFile file : files) {
//获取原始文件名
String filename = file.getOriginalFilename();
//获取文件大小
int size = (int) file.getSize();
//保存路径
String tempPath = ConfigProperties.getProp(Constants.UPLOAD_LOCATION);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(System.currentTimeMillis());
String format = simpleDateFormat.format(date);
String[] split = format.split("-");
//添加年月日目录
tempPath = tempPath+"/"+split[0]+"/"+split[1]+"/"+split[2];
File fileDir = new File(tempPath);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
//保存图片信息的实体类,根据自己情况编写
Attachment attachment = new Attachment();
attachment.setAttachmentId(U.id());
attachment.setDataId(dataId);
attachment.setName(filename);
String url = tempPath+"/"+System.currentTimeMillis()+filename.substring(filename.indexOf("."));
attachment.setUrl("/file/download"+url.substring(tempPath.indexOf("upload/")+6));
attachment.setFileSize(size);
attachmentService.insert(attachment);
//上传文件
BufferedOutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(new File(url)));
//上传文件
out.write(file.getBytes());
out.flush();
logger.debug("上传文件的原始文件名:" + filename);
logger.debug("上传文件的路径:"+url);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
}
return "forward:/device/save";
}
}
文章浏览阅读1.6w次,点赞8次,收藏18次。已解决(selenium操作火狐浏览器报错)TypeError: __init__() got an unexpected keyword argument ‘firefox_options‘_typeerror: __init__() got an unexpected keyword argument 'chrome_options
文章浏览阅读315次。python实现代码# -*- coding: utf-8 -*-import mathdef get_average(records):"""平均值"""return sum(records) / len(records)def get_variance(records):"""方差 反映一个数据集的离散程度"""average = get_average(records)return sum(..._pycharm方差计算代码
文章浏览阅读902次。一、前提:cookie是保存在本地,用来记录用户信息,最典型的作用是判断用户是否已经登录。如果一个接口,需要用户登录后,才能进行操作,如下,修改用户信息那么这时候,就需要用到cookie去识别这个登录的用户,因为要修改的是这个登录的用户的信息。二、使用1、jmeter.properties 中 将CookieManager.save.cookies 设置为true。完成后重启jmet..._jmeter中post请求如何更改每次的用户名
文章浏览阅读7.5k次,点赞2次,收藏2次。虚拟环境的好处:搭建独立的python运行环境,不与其他产生冲突虚拟环境有助于包的管理和防止版本冲突3.删除卸载方便虚拟环境的搭建:1.进入python的Scripts下,执行:pip3 install virtualenv2.选择建立虚拟环境的文件夹,我这边是直接在D盘software下面创建了一个virtualenv,如图:image.png3.创建虚拟环境:virtualenv --no-s..._pycharm没有venv怎么办
文章浏览阅读2.3k次。--pod install时报错,且错误提示中有“ffi”字眼,提示错误:/Library/Ruby/Gems/2.6.0/gems/ffi-1.15.3/lib/ffi/library.rb:275: [BUG] Bus Error at 0x00000001042fc000 ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.arm64e-darwin20]解决方案:juejin.cn/post/698064…--pod repo upd_oc cocospods 安装依赖库之后不能使用模拟器了
文章浏览阅读540次。工欲善其事必先利其器,趁手的工具会使我们开发事半功倍。市面上的编辑器我用过许多,编辑器使用经历Notepad++,(开源)这个应该是最轻量级的吧,查看代码还好,编辑代码就算了官网地址:https://notepad-plus-plus.org/Brackets,(开源)这个也不错,github-star:30k了,上次发布版本是6月..._vscode nextick
文章浏览阅读95次。前言:织梦程序是最知名的cms程序,使用广泛,但很多朋友对织梦还不太熟悉,通过工单分析得知,经常容易出现本文中的问题,本次统一整理出来,希望对新手朋友有帮助,本文写的非常详细,请仔细阅读,一、描述:“dedecms错误警告:连接数据库失败,可能数据库密码不对或数据库服务器出错”如图:分析:用织梦程序制作的站点做迁移服务器后容易出现这个问题,原因是程序中设置的数据库连接信息有误连接不到数据库,解决方..._西部数码支不支持织梦的程序
文章浏览阅读295次。上午在win7下安装MYSQL,只到“net start mysql”这一步报错:3534的错误:于是在百度中搜索关键字“mysql服务无法启动3534”。参考以下两个链接中的方法,解决了3534启动失败的问题:mysql服务无法启动3534错误。http://jingyan.baidu.com/article/219f4bf7e978fcde442d38a9.htmlhttp://blog.cs..._免安装mysql启动失败
文章浏览阅读5.1w次,点赞58次,收藏295次。最近在做项目和复习的时候,用了不少流程图软件给我帮了大忙,所以今天就来分享分享你在网上搜索一下流程图软件,能找到很多很多:但这些软件多数并不是专门绘制流程图的软件,它可能是一些思维导图软件、或者说一些产品交互原型图软件,使用时或多或少有些麻烦。而且,普遍这些软件缺点也很多,比如:只有在线版:ProcessOn(https://www.processon.com)导出功能收费:迅捷流程图软件体积庞大:VISIO就没有一款简单易用、绿色免费的流程图软件吗?阿虚花了不少时间,挨着_好用的流程图软件免费
文章浏览阅读1.4k次。cd /data/install_pkgwget https://github.com/azkaban/azkaban/archive/3.50.0.tar.gztar -zxvf 3.50.0.tar.gzvi /data/install_pkg/azkaban-3.50.0/azkaban-common/src/main/java/azkaban/utils/EmailMessage..._centos6.9 azkaban
文章浏览阅读586次,点赞23次,收藏30次。博主介绍:CSDN特邀作者、985计算机专业毕业、某互联网大厂高级全栈开发程序员、码云/掘金/华为云/阿里云/InfoQ/StackOverflow/github等平台优质作者、专注于Java、小程序、前端、python等技术领域和毕业项目实战,以及程序定制化开发、全栈讲解、就业辅导、面试辅导、简历修改。精彩专栏 推荐订阅2023-2024年最值得选的微信小程序毕业设计选题大全:100个热门选题推荐2023-2024年最值得选的Java毕业设计选题大全:500个热门选题推荐。
文章浏览阅读944次。由于需要用pyqt给yolov8做一个界面,而ultralytics一层嵌一层,不是很好用,所以对它的这个源码进行精简,具体代码我放到了这里,ultralytics使用的版本是8.0.54。具体代码如下,需要根据自己的情况来修改data的配置文件以及权值文件,在代码的49和50行。_from ultralytics.utils.plotting import annotator, colors, save_one_box modul