layui實(shí)現(xiàn)圖片虛擬路徑上傳,預(yù)覽和刪除的例子
效果如下所示:
前端:
<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">單據(jù)上傳(可上傳多張)</button> <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;width: 88%"> 預(yù)覽圖: <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="單據(jù)" 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());//關(guān)閉上傳提示窗口 //上傳完畢 $('#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") { //鼠標(biāo)懸浮 $(this).children(".info").fadeIn("fast"); $(this).children(".handle").fadeIn("fast"); } else if (event.type === "mouseleave") { //鼠標(biāo)離開(kāi) $(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: "單據(jù)", 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();//獲取時(shí)間戳 String realPath = fileUploadPathUrl;//項(xiàng)目路徑 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實(shí)現(xiàn)圖片虛擬路徑上傳,預(yù)覽和刪除的例子就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
js實(shí)現(xiàn)支持手機(jī)滑動(dòng)切換的輪播圖片效果實(shí)例
這篇文章主要介紹了js實(shí)現(xiàn)支持手機(jī)滑動(dòng)切換的輪播圖片效果,實(shí)例分析了javascript實(shí)現(xiàn)圖片切換的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04JavaScript設(shè)計(jì)模式學(xué)習(xí)之“類式繼承”
這篇文章主要介紹了JavaScript設(shè)計(jì)模式學(xué)習(xí)之“類式繼承”,本文直接用代碼實(shí)例講解類式繼承的實(shí)現(xiàn)方法,需要的朋友可以參考下2015-03-03javascript實(shí)現(xiàn)移動(dòng)端上傳圖片功能
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)移動(dòng)端上傳圖片功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08JavaScript導(dǎo)出CSV文件不完整的問(wèn)題解決方法
在JavaScript中處理CSV文件時(shí),需要特別注意一些特殊字符,例如逗號(hào)、雙引號(hào)、換行符等,這些字符可能會(huì)影響CSV文件的解析,導(dǎo)致數(shù)據(jù)錯(cuò)亂,所以本文給大家介紹了如何解決JavaScript導(dǎo)出CSV文件不完整的問(wèn)題,需要的朋友可以參考下2024-06-06javascript實(shí)現(xiàn)的一個(gè)自定義長(zhǎng)度的文本自動(dòng)換行的函數(shù)。
javascript實(shí)現(xiàn)的一個(gè)自定義長(zhǎng)度的文本自動(dòng)換行的函數(shù)。...2007-08-08JavaScript中添加監(jiān)聽(tīng)句柄的方式
這篇文章主要介紹了JavaScript中添加監(jiān)聽(tīng)句柄的方式,監(jiān)聽(tīng)就是觸發(fā)某事件之后做出的響應(yīng),監(jiān)聽(tīng)句柄是觸發(fā)某相應(yīng)的條件,下面關(guān)于添加監(jiān)聽(tīng)句柄的方式的詳細(xì)內(nèi)容,需要的朋友可以參考一下,希望對(duì)你有所幫助2022-02-02通過(guò)javascript實(shí)現(xiàn)掃雷游戲代碼實(shí)例
這篇文章主要介紹了通過(guò)javascript實(shí)現(xiàn)掃雷游戲代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02JavaScript通過(guò)使用onerror設(shè)置默認(rèn)圖像顯示代替alt
這篇文章主要介紹了JavaScript通過(guò)使用onerror設(shè)置默認(rèn)圖像顯示代替alt的相關(guān)資料,需要的朋友可以參考下2016-03-03