layui实现图片虚拟路径上传,预览和删除的例子
作者:wenshao145 发布时间:2024-07-26 08:53:10
标签:layui,图片,上传,预览,删除
效果如下所示:
前端:
<style type="text/css">
#detailTbody tr:hover {
background: #fff;
}
.layui-form-label {
width: 110px;
}
.uploader-list {
margin-left: -15px;
}
.uploader-list .info {
position: relative;
margin-top: -25px;
background-color: black;
color: white;
filter: alpha(Opacity=80);
-moz-opacity: 0.5;
opacity: 0.5;
width: 100px;
height: 25px;
text-align: center;
display: none;
}
.uploader-list .handle {
position: relative;
background-color: #ff6a00;
color: white;
filter: alpha(Opacity=80);
-moz-opacity: 0.5;
width: 100px;
text-align: right;
height: 18px;
margin-bottom: -18px;
display: none;
}
.uploader-list .handle span {
margin-right: 5px;
}
.uploader-list .handle span:hover {
cursor: pointer;
}
.uploader-list .file-iteme {
margin: 12px 0 0 15px;
padding: 1px;
float: left;
}
</style>
<div class="layui-upload">
<button type="button" class="layui-btn layui-btn-warm" id="test2">单据上传(可上传多张)</button>
<blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;width: 88%">
预览图:
<div class="layui-upload-list uploader-list" style="overflow: auto;" id="uploader-list">
<div id="" class="file-iteme" th:each="img :${data.orderVoucher}">
<div class="handle"><i class="layui-icon" style="color: white;margin-right: 40%"></i>
</div>
<img th:src="${img}" alt="单据" width="100" height="100" onclick="showBig(this)">
</div>
</div>
</blockquote>
</div>
js:
layui.use(['form', 'layer', 'laydate', 'upload'], function () {
$ = layui.jquery;
var form = layui.form,
layer = layui.layer,
laydate = layui.laydate,
upload = layui.upload;
//多图片上传
upload.render({
elem: '#test2'
, url: '/psi/order/uploadImg'
, multiple: true
, before: function (obj) {
layer.msg('图片上传中...', {
icon: 16,
shade: 0.01,
time: 0
})
}
, done: function (res) {
layer.close(layer.msg());//关闭上传提示窗口
//上传完毕
$('#uploader-list').append(
'<div id="" class="file-iteme">' +
'<div class="handle"> <i class="layui-icon" style="color: white ;margin-right: 40%"></i></div>' +
'<img style="color: white;width: 100px;height: 100px" onclick="showBig(this)" src=' + res.url + ' >' +
'</div>'
);
}
});
});
$(document).on("mouseenter mouseleave", ".file-iteme", function (event) {
if (event.type === "mouseenter") {
//鼠标悬浮
$(this).children(".info").fadeIn("fast");
$(this).children(".handle").fadeIn("fast");
} else if (event.type === "mouseleave") {
//鼠标离开
$(this).children(".info").hide();
$(this).children(".handle").hide();
}
});
$(document).on("click", ".file-iteme .handle", function(event){
$(this).parent().remove();
})
})
function showBig(obj) {
var url = (obj.src);
var index = layer.open({
type: 2,
content: url,
area: ['100%', '100%'],
title: "单据",
maxmin: true,
closeBtn: 1
});
layer.full(index);
}
controller层
@RequestMapping(value = "/uploadImg")
@ResponseBody
public Map<String,Object> uploadImg(MultipartFile file,HttpServletRequest request){
Map<String,Object> data = new HashMap<>();
String url = "";
if (!file.isEmpty()){
url = FileUploadUtil.saveImage(file,"orderVoucher",request);
}
data.put("url",url);
return data;
}
FileUploadUtil类
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Date;
public class FileUploadUtil {
public static String fileUploadPathUrl="D:\\svnproject\\wechatprintingPicture";
/**
* 图片读取存放获取路径
*
* @param file 文件
* @param fileName 文件存放的目录名
* @return
*/
public static String saveImage(MultipartFile file, String fileName, HttpServletRequest requestFileUploadUtil) {
long timestamp = new Date().getTime();//获取时间戳
String realPath = fileUploadPathUrl;//项目路径
String newFileName = timestamp + "" + file.getOriginalFilename(); //file.getOriginalFilename()是获取原始图片的拓展名,newfileName新的文件名字
String path = realPath + "/" + fileName;
String newPath = path + "/" + newFileName;////图片存放的位置路径
File filePath = new File(path + "/");
if (!filePath.exists()) {
filePath.mkdirs();
}
if (!file.isEmpty()) {
BufferedOutputStream out = null;
try {
out = new BufferedOutputStream(
new FileOutputStream(new File(newPath)));
out.write(file.getBytes());
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
String url = requestFileUploadUtil.getScheme() + "://" + requestFileUploadUtil.getServerName() + ":" + requestFileUploadUtil.getServerPort() + requestFileUploadUtil.getContextPath() + "/" + fileName + "/" + newFileName;
return url;
}
}
yml虚拟路径配置
spring:
resources:
static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.uploadPath}
web:
uploadPath: D:/svnproject/wechatprintingPicture
来源:https://blog.csdn.net/wenshao145/article/details/89176321


猜你喜欢
- 桑基图桑基图(Sankey diagram),即桑基能量分流图,也叫桑基能量平衡图。它是一种特定类型的流程图,图中延伸的分支的宽度对应数据流
- 今天自己写个楼层效果,有一点烦躁,小地方犯错误。各位大神来修改不足啊!!!<!DOCTYPE html><html lan
- 先看示例i=1menu=["B超室","化验室"]user="张来,黎明,常冒
- 之前在懒懒分会上分享的一点关于border画小图的内容, 完整的ppt在这里.原理css盒模型一个盒子包括: margin+border+p
- 问题:自己写了个dataloader,为了部署方便,用OpenCV的接口进行数据读取,而没有用PIL,代码大致如下: &nbs
- 当服务器必须提供与两个或更多个网络或网络子网的连接时,典型的方案是使用多宿主计算机。此计算机通常位于外围网络(也称为 DMZ、外围安全区域或
- 第一步:更改setting.py中的DATABASES# 配置数据库DATABASES = { 'default'
- 看了下网上有很多关于模拟登录淘宝,但是基本都是使用scrapy、pyppeteer、selenium等库来模拟登录,但是目前我们还没有讲到这
- 当然考虑到浏览器(特别指IE)糟糕的js执行能力,动画效果又要受到影响。 浏览器中的动画效果主要依靠js来动态改变Dom元素外观来形成。不过
- JSON [ JavaScript Object Notation ]:JavaScript 对象表示法。它是一种轻量级的数据交换格式。 J
- 本文实例讲述了Python实现拼接多张图片的方法。分享给大家供大家参考。具体分析如下: 这里所述计划实现如下操作: ①
- 一、适配器模式适配器,顾名思义是一种万能的接口,达到万能转换的效果。适配器模式,定义一个适配器类,并且在该类中定义了适配器接口,这些适配接口
- 使用ktl工具实现mysql向mysql同步数据1.新建作业步骤2.完善作业步骤,主要是完成作业中的转换工作3.首先要确定数据来源库,也就是
- 本文实例讲述了Python strip()函数的正则表达式实现方法。分享给大家供大家参考,具体如下:题目如下:写一个函数,它接受一个字符串,
- 一、前言前两篇博客讲解了爬虫解析网页数据的两种常用方法,re正则表达解析和beautifulsoup标签解析,所以今天的博客将围绕另外一种数
- Demo里的三种方法:方法1是两层div,兼容FF3.1a+, Safari 3+, Chrome, IE6/7方法2是两层div,除了IE
- 为何使用函数最大化代码的重用和最小化代码冗余流程的分解编写函数>>def语句在Python中创建一个函数是通过def关键字进行的
- 在读取文件时将整数变量读成了字符串, 或者需要转换列属性时,通过方法astypePython中举例:dataframe.numbers=da
- 有时一些网页对源码进行了加密,我们很难找到类似像“onselectstart="return false"”这样的代码,
- 在开发web的时候,如果是以前已存在的项目,项目下载下来后,为了使用测试库的数据,会直接将整个测试库(如sqlite3)拿到本机来。这种情况