layui中如何实现图片虚拟路径上传功能
这篇文章给大家分享的是有关layui中如何实现图片虚拟路径上传功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
十余年的大理州网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网整合营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整大理州建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“大理州网站设计”,“大理州网站推广”以来,每个客户项目都认真落实执行。
效果如下所示:
前端:
预览图:
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( '' + '' ); } }); }); $(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 MapuploadImg(MultipartFile file,HttpServletRequest request){ Map 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
感谢各位的阅读!关于“layui中如何实现图片虚拟路径上传功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
新闻标题:layui中如何实现图片虚拟路径上传功能
转载来源:http://lswzjz.com/article/gpdcso.html