轻松实现图片上传并回显

http://narutolby.iteye.com/blog/1449908

  图片上传及文件上传,前端html实现文件上传和普通的向后台传参数不同,必须用到form表单,同时form标签必须加上enctype="multipart/form-data"属性,method必须为post,表单中的input的type为file,这样前端上传文件的工作就完成了,这里后台文件的存储不做详细的介绍,因为用到的是struts2,文件上传的功能已经封装好了,但是还是需要注意几点,action中类型为File的属性名必须和input标签的name相同,同时action中必须有contentType和fileName属性,这里不是本文讲的重点就不做详细介绍了。 
  大家知道,form表单的post请求是同步请求,如果没有指定target属性,默认情况下提交后会刷新页面,想要做到提交请求后不页面不动即能回显上传的图片,这就需要一些处理,这里我用到了隐藏的iframe,将form表单的target指向iframe的名字,这样form表单提交后返回的结果将在iframe中显示。

  大家知道,form表单的post请求是同步请求,如果没有指定target属性,默认情况下提交后会刷新页面,想要做到提交请求后不页面不动即能回显上传的图片,这就需要一些处理,这里我用到了隐藏的iframe,将form表单的target指向iframe的名字,这样form表单提交后返回的结果将在iframe中显示。  
Html代码   收藏代码
  1.   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title>Upload Image Demo</title>  
  6. <script type="text/javascript" src="js\jquery-1.7.1.min.js"></script>  
  7. <script type="text/javascript">  
  8.    function upload(){  
  9.        $('form').submit();  
  10.        $('img').attr('src','img/vote_img_loading.png')  
  11.    }  
  12.    $(document).ready(function(){  
  13.        $(':input[type=file]').change(upload);  
  14.    });  
  15.      
  16.    function showPic(path){  
  17.        $('img').attr('src','file/'+path).next('span').css('visibility','visible');  
  18.          
  19.    }  
  20.   
  21. </script>  
  22. <style type="text/css">  
  23. .pic {  
  24.     position: relative;  
  25.     width: 100px;  
  26.     height: 100px;  
  27.     float: left;  
  28.     display: inline;  
  29.     margin: 0 5px 0 0;  
  30.     cursor: pointer;  
  31.     overflow: hidden;  
  32. }  
  33.   
  34. .pic span {  
  35.     position: relative;  
  36.     color: white;  
  37.     margin: -20px 0 0;  
  38.     display: block;  
  39.     height: 20px;  
  40.     width: 100px;  
  41.     text-align: center;  
  42.     overflow: hidden;  
  43.     line-height: 20px;  
  44.     background: url(img/template_list_titlebg.png) ;  
  45. }   
  46.   
  47. .pic form {  
  48.     position: absolute;  
  49.     width: 100px;  
  50.     height: 100px;  
  51.     overflow: hidden;  
  52.     z-index: 10;  
  53.     margin: -100px 0 0;  
  54. }  
  55.   
  56. .pic input {  
  57.     font-size: 100px;  
  58.     cursor: pointer;  
  59.     -moz-opacity: 0;  
  60.     filter: alpha(opacity = 0);  
  61.     opacity: 0;  
  62.     background: none;  
  63.     border: none;  
  64.     margin: -10px 0 0 -650px 9;  
  65. }  
  66. </style>  
  67. </head>  
  68. <body>  
  69.     <iframe style="display:none" name="if">  
  70.       
  71.     </iframe>  
  72.     <div class="pic">  
  73.         <img height="100" width="100" src="img/update_pic.png"><span  
  74.             style="visibility: hidden">重新上传</span>  
  75.         <form enctype="multipart/form-data" method="POST" action="upload.action" target="if">  
  76.             <input type="file" name="upload"/>  
  77.         </form>  
  78.     </div>  
  79. </body>  
  80. </html>  



关于iframe中显示的内容是图片回显的关键,以下是返回的内容:  
Html代码   收藏代码
  1. <html>  
  2. <head>  
  3. </head>  
  4. <body>  
  5.  <script type="text/javascript">  
  6.  var pid = window.location.search.split('=')[1];  
  7.  top.window.showPic(pid);  
  8.    
  9.  </script>  
  10. </body>  
  11. </html>  


   可以看到返回的内容很简单,就是一段javascript代码,因为当form表单提交后,我做了url重定向,并把图片的存储路径作为重定向url的参数,这段javascript代码首先从iframe的location中获得图片的存储路径,然后调用父窗口的showPic函数,并将图片存储路径传入函数,showPic函数是写在父床口中的,为什么可以这么做,因为子窗口可以调用父窗口的函数,但是反过来父窗口调用子窗口的函数是不被允许的。showPic函数如下: 

Js代码   收藏代码
  1. function showPic(path){  
  2.        $('img').attr('src','file/'+path).next('span').css('visibility','visible');  
  3.          
  4.    }  

    它的作用就是将img标签的src指向传过来的图片路径,这时候浏览器就回去加载这个图片,这样就轻松的实现了图片上传并迅速回显的功能,我附上了实现这个功能的整个工程,是一个eclipse的maven工程,需要通过本机maven命令下载依赖包。
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-select 是 Element UI 提供的下拉选择器组件,本身并不支持上图片图片的功能。你可能需要结合其他 Element UI 组件,如 el-upload 和 el-image,来实现这个功能。 以下是一个示例代码,可以实现在 el-select 中选择图片,并在下拉框中图片: ```html <template> <el-select v-model="selectedImg" placeholder="Select image"> <el-option v-for="(img, index) in images" :key="index" :label="img.name" :value="img.url"> <el-image :src="img.url" :fit="fit" /> </el-option> <el-option> <el-upload class="upload-demo" :action="uploadUrl" :show-file-list="false" :on-success="handleSuccess" > <el-button size="small" type="primary">Click to upload</el-button> </el-upload> </el-option> </el-select> </template> <script> export default { data() { return { images: [ { name: 'Image 1', url: 'https://via.placeholder.com/150' }, { name: 'Image 2', url: 'https://via.placeholder.com/150' }, { name: 'Image 3', url: 'https://via.placeholder.com/150' }, ], selectedImg: '', fit: 'cover', uploadUrl: '/api/upload', // replace with your own upload API URL }; }, methods: { handleSuccess(res) { this.images.push({ name: res.name, url: res.url }); }, }, }; </script> ``` 这个示例中,我们使用 el-option 组件来展示图片,并在其中嵌入 el-image 组件来图片。同时,在最后一个 el-option 中,我们使用 el-upload 组件来实现图片功能。上成功后,会调用 handleSuccess 方法将新上图片信息添加到 images 数组中,从而实现图片的动态更新。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值