html如何實現頁面上傳圖片並且能進行展示的範例程式碼分享
html
上傳
頁面
html页面:
本功能实现图片上传并显示,点击“查看”按钮也显示图片
<p class="form-group"> <label class="col-md-2 control-label">缩略图</label> <p class="col-md-2"> <input type="hidden" name="news.thumbnail_atta_id" value="${news.thumbnail_atta_id!}" id="thumbnail_atta_id"/> <p class="col-md-2"> @ if (!isEmpty(news.thumbnail_atta_id)) { <p class="row" style="margin-bottom:5px;"> <button type="button" class="btn btn-primary" id="showThumbnail">查看</button> </p> @ } <p class="row"> <span class="btn btn-success fileinput-button"> <i class="glyphicon glyphicon-plus"></i> <span>上传...</span> <input id="fileupload" type="file" data-url="/atta/upload/news-thumbnail" multiple> </span> </p> </p> <p> <img src="" id="thumbnail" width="150" height="100" style="display:none;"/> </p> </p> </p>
登入後複製
js代码:
<script type="text/JavaScript"> //缩略图文件上传 $('#fileupload').fileupload({ dataType: 'json', add: function(e, data) { var acceptFileTypes = /(\.|\/)(gif|jpe?g|png)$/i; if(data.originalFiles[0]['type'].length && !acceptFileTypes.test(data.originalFiles[0]['type'])) { alert('请上传图片格式的文件!') } else { data.submit(); } }, done: function (e, data) { $('#thumbnail_atta_id').val(data.result.id); $('#thumbnail').attr('src', data.result.url).show(); } }); //查看缩略图 $('#showThumbnail').click(function(){ var src = $('#thumbnail').attr('src'); var id=$('#thumbnail_atta_id').val(); if (!src) { $.get('/atta/show/${news.thumbnail_atta_id!}', function(res) { var url=localurl+res[0].url; //var url = "www.baidu.com/img/bd_logo1.png"; alert(url); $('#thumbnail').attr('src', url).show(); }, 'json') } }) </script>
登入後複製
Java代码:
Controller类:
/** * 上传并返回附件ID及URL */ public void upload() { UploadFile file = getFile(); String fileName = file.getFileName(); String fileSuffix = getSuffix(fileName); String module = getPara(0); String category = getPara(1); String id = AttachmentService.me.uploadFile(file, module, category); Map<String, Object> json = new HashMap<String, Object>(); json.put("id", id); json.put("url", "/upload/" + category + "/" + module + "/" + id + fileSuffix); json.put("name", fileName); renderJson(json); } /** * 返回给定ID的附件的访问URL */ public void show() { String ids = getPara(); if (null == ids) { ids = getPara("ids"); } String[] idArr = ids.split(","); StringBuffer sql = new StringBuffer(); sql.append(" select * from attachment where id in ( "); for (int index = 0; index < idArr.length; index++) { sql.append(" ? "); if (index != idArr.length - 1) { sql.append(" , "); } } sql.append(" ) "); List<Map<String, Object>> json = new ArrayList<Map<String,Object>>(); List<Attachment> list = Attachment.me.find(sql.toString(), (Object[])idArr); //转换成JsonArray String url = null; for (Attachment atta : list) { url = getAttaURL(atta); Map<String, Object> item = new HashMap<String, Object>(); item.put("id", atta.getStr("id")); item.put("url", url); item.put("name", atta.getStr("name")); json.add(item); } renderJson(json); } private String getAttaURL(Attachment atta) { StringBuffer path = new StringBuffer(); path.append(File.separator + "upload" + File.separator); path.append(atta.getStr("category")); path.append(File.separator); path.append(atta.getStr("module")); path.append(File.separator); path.append(atta.getStr("id")); path.append(getSuffix(atta.getStr("name"))); return path.toString(); } private String getSuffix(String name) { return name.replaceAll(".*(\\..*)", "$1"); }
登入後複製
service类:
/** * 上传文件 * @param file uploadFile * @param module 模块,如news/cases/doctor/ * @param category 分类,如缩略图thumbnail/头像avatar/幻灯片ppt * @return 附件主键 * @throws IOException */ public String uploadFile(UploadFile file, String module, String category) { String attachmentId = CommonUtils.getUUID(); File f = file.getFile(); new Attachment().set("id", attachmentId) .set("name", file.getOriginalFileName()) .set("size", f.length()) .set("content_type", file.getContentType()) .set("module", module) .set("category", category) .set("upload_time", new Date()) .save(); try { //重命名 String dirPath = file.getSaveDirectory(); File renamedFile = new File(dirPath + attachmentId + file.getOriginalFileName().replaceAll(".*(\\..*)", "$1")); FileUtils.moveFile(f, renamedFile); //PPT需要转换成图片,拷贝一份到转换目录(/upload/trans/ppt) if (category.equals("ppt")) { FileUtils.copyFileToDirectory(renamedFile, new File(dirPath + "trans/ppt/"), true); } //拷贝到对应的目录下 FileUtils.moveToDirectory(renamedFile, new File(dirPath + category + File.separator + module), true); } catch(Exception e) { e.printStackTrace(); } return attachmentId; }
登入後複製
以上是html如何實現頁面上傳圖片並且能進行展示的範例程式碼分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影:貝殼謎語解決方案
2 週前
By DDD
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
1 個月前
By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)
