uploadfile和Image实现图片预览

 this.FileUpload1.Attributes.Add("onchange", "document.getElementById('" + this.Image1.ClientID + "').src=document.getElementById('" + this.FileUpload1.ClientID + "').value");

<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<script type="text/javascript" src="http://www.cssrain.cn/demo/JQuery+API/jquery-1[1].2.1.pack.js"></script>
<script language="javascript">
$(
function(){
    
var ei = $("#large");
    ei.hide();
    $(
"#img1, img").mousemove(function(e){
        ei.css({top:e.pageY,left:e.pageX}).html(
'<img style="border:1px solid gray;" src="' + this.src + '" />').show();
    }).mouseout( 
function(){
        ei.hide();
    })
    $(
"#f1").change(function(){
        $(
"#img1").attr("src","http://www.cnblogs.com/Pynix/admin/file:///"+$("#f1").val());
    })
});
</script>
<style type="text/css">
    #large
{position:absolute;display:none;z-index:999;}
</style>
</head>
<body>

上传预览图片:
<br>
<input id="f1" name="f1" type="file" /><br>
<img id="img1" width="120" height="60" src="http://www.cssrain.cn/skins/tim/logo-jq.gif">

<div id="large"></div>

<br><br><br><br><br><br>
鼠标滑过预览图片:
<br>
<img width="120" height="60" src="http://www.cssrain.cn/skins/tim/logo-jq.gif"><br>
<img width="120" height="60" src="http://www.cssrain.cn/demo/JQuery+API/logo-json.gif"><br>
</body>
</html>



 
    function getFullPath(obj) {    //得到图片的完整路径 
     if (obj) { 
         //ie 
         if (window.navigator.userAgent.indexOf("MSIE") >= 1) { 
             obj.select(); 
             return document.selection.createRange().text; 
         } 
         //firefox 
         else if (window.navigator.userAgent.indexOf("Firefox") >= 1) { 
             if (obj.files) { 
                 return obj.files.item(0).getAsDataURL(); 
             } 
             return obj.value; 
         } 
         return obj.value; 
     } 
    
      $("#FileUpload1").change(function () { 
         var strSrc = this.value; 
         img = new Image(); 
         img.src = getFullPath(this); 
         //验证上传文件格式是否正确 
         var pos = strSrc.lastIndexOf("."); 
         var lastname = strSrc.substring(pos, strSrc.length) 
         if (lastname.toLowerCase() != ".jpg") { 
             alert("您上传的文件类型为" + lastname + ",图片必须为 JPG 类型"); 
             return false; 
         } 
         //验证上传文件宽高比例 
 
         if (img.height / img.width > 1.5 || img.height / img.width < 1.25) { 
             alert("您上传的图片比例超出范围,宽高比应介于1.25-1.5"); 
             return; 
         } 
         //验证上传文件是否超出了大小 
         if (img.fileSize / 1024 > 150) { 
             alert("您上传的文件大小超出了150K限制!"); 
             return false; 
         }
         $("#Image1").attr("src", getFullPath(this)); 
         })

<html>      
<head>      
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">      
<title>带有图片预览功能的上传表单</title>      
<script>      
function viewmypic(imgfile) {       
if (imgfile.value){
    document.getElementById("showimg").src=imgfile.value;       
 
}       
}       
</script>      
</head>      
<body>      
<center>      
<form >      
<input name="imgfile" type="file" id="imgfile" size="40" οnchange="viewmypic(this.form.imgfile);" />      
<br />      
</form>      
<img name="showimg" id="showimg" src="My Pictures/1.png"  alt="预览图片" />      
<br />      
</div>      
<div style="display:none">      
</div>      
</center>      
</body>      
</html>

转载于:https://www.cnblogs.com/50614090/archive/2011/04/17/2019160.html

以下是基于SSM和Vue实现图片上传并预览的代码示例: 前端代码: ``` <template> <div> <input type="file" ref="fileInput" accept="image/*" @change="handleFileUpload"> <img v-if="imageUrl" :src="imageUrl" style="max-width: 100%;"> </div> </template> <script> export default { data() { return { imageUrl: '', file: null } }, methods: { handleFileUpload() { this.file = this.$refs.fileInput.files[0]; this.previewImage(this.file); this.uploadFile(this.file); }, previewImage(file) { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = e => { this.imageUrl = e.target.result; } }, async uploadFile(file) { const formData = new FormData(); formData.append('file', file); try { const response = await this.$axios.post('/upload', formData); console.log(response.data); } catch (error) { console.log(error); } } } } </script> ``` 后端代码: ``` @Controller public class UploadController { @PostMapping("/upload") @ResponseBody public String upload(@RequestParam("file") MultipartFile file) throws IOException { // 保存文件到服务器 File uploadFile = new File("/path/to/upload/" + file.getOriginalFilename()); file.transferTo(uploadFile); // 返回文件访问路径 return "http://your.domain.com/upload/" + file.getOriginalFilename(); } } ``` 其中,前端代码使用了Vue.js框架和axios库,后端使用了Spring MVC框架。在前端,我们使用`<input type="file">`元素来让用户选择上传的图片文件,然后使用FileReader API来预览图片;在上传文件时,我们将文件封装为FormData对象,并使用axios库发送POST请求到后端的/upload接口;在后端,我们使用Spring MVC框架的@RequestParam注解来接收上传的文件,并将文件保存到服务器上,最后返回文件的访问路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值