1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > html如何实现页面上传图片并且能进行展示的示例代码分享

html如何实现页面上传图片并且能进行展示的示例代码分享

时间:2020-06-30 22:08:19

相关推荐

html如何实现页面上传图片并且能进行展示的示例代码分享

web前端|html教程

html,上传,页面

web前端-html教程

html页面:

仿airbnb源码,覆盖安装.ubuntu,tomcat联记事本,ppt爬虫攻略,互联网金融 php,十堰网店seo推广开户lzw

本功能实现图片上传并显示,点击“查看”按钮也显示图片

html源码之家,vscode 编译lua,ubuntu固态,tomcat 时间区域,sqlite日期数据存放,8x插件,当前流行的前端框架及优势,爬虫爬取nike官网,php 验证码无法显示,鹰潭谷歌seo公司,织梦网站地图制作教程,网页首页滚动特效代码,在线相册网站 模板lzw

@ if (!isEmpty(news.thumbnail_atta_id)) {

asp 订单 源码,ubuntu设置空白ip,tomcat面试题简答题,网络爬虫抓取技术,php没有被赋值的值为0,我去seolzw

@ }

上传...

js代码:

//缩略图文件上传$(#fileupload).fileupload({dataType: json,add: function(e, data) {var acceptFileTypes = /(\.|\/)(gif|jpe?g|png)$/i;if(data.originalFiles[0][ ype].length && !acceptFileTypes.test(data.originalFiles[0][ ype])) {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 = "/img/bd_logo1.jpg" alt="html如何实现页面上传图片并且能进行展示的示例代码分享" title="html如何实现页面上传图片并且能进行展示的示例代码分享";alert(url);$(#thumbnail).attr(src, url).show();}, json)}})

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 json = new HashMap();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> json = new ArrayList<Map>();List list = Attachment.me.find(sql.toString(), (Object[])idArr);//转换成JsonArrayString url = null;for (Attachment atta : list) {url = getAttaURL(atta);Map item = new HashMap();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;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。