谷歌异步上传图片预览

var divshowphotoid = "divshowphotoid";
function fileuploadinit(property){
   alert("action   "+property.action+"   fileid   "+property.fileid);
   var iframename = "photoiframe";
   $("#"+property.fileid).before("<iframe name='"+iframename+"' style='display:none;'></iframe>");
   //创建显示图片的区域
   alert("1");
   $("#"+property.fileid).after("<div id='"+divshowphotoid+"'></div>");   
   // 创建Form
   alert("2");
        var form = $('<form></form>');
        // 设置属性
        form.attr('action', property.action);
        var method = "POST";
        form.attr("method", method);
        enctype="multipart/form-data"
        form.attr("enctype",enctype);
        // form的target属性决定form在哪个页面提交
        form.attr("target", iframename);
        //复制元素
        alert("3");
        var oldElement = $("#" + property.fileid);  
        var newElement = $(oldElement).clone();  
        $(oldElement).attr("id", "test");  
        $(oldElement).before(newElement);  
        $(oldElement).appendTo(form); 
        alert("4");
        // 提交表单
        form.submit();
        alert("5");
        // 注意return false取消链接的默认动作
       //return false; 
}
function UpdateMsg(fileurl){//此函数用来提供给提交到的页面如upload.ashx输出js的回调,更新当前页面的信息
              if(fileurl==''){
                   alert('未上传文件!');
                   return false;
              }
              var obj=eval(fileurl);
                  
              $.each(obj,function(i){
                    //将后台传回的信息打印到页面中
                    alert(obj[i].url);
                   if(i%3==0){
                       $("#"+divshowphotoid).after('<img src="'+obj[i].url+'" width="155" height="95" /><br/>');
                   }else{
                       $("#"+divshowphotoid).after('<img src="'+obj[i].url+'" width="155" height="95" />');
                   }
                   
          });

    }





<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'photo.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script charset="utf-8" src="js/jquery-1.7.2.min.js"></script>
<script charset="utf-8" src="js/photoupload.js"></script>
  </head>
  
  <body>   
    选择文件:<input type="file" id="multiupfile" name="multiupfile"    οnchange="fileuploadinit({action:'/SpringMvcTest/jquery/multiphoto.html',fileid:'multiupfile'})"/><br/>
  
  </body>
</html>




@RequestMapping(value="/multiphoto")
public void multiphoto(HttpServletRequest request,HttpServletResponse response,MultipartHttpServletRequest fileRequest) throws IOException{
System.out.println("******************************jquery/multiphoto");
//指定输出头和编码
response.setCharacterEncoding("UTF-8"); 
response.setContentType("text/html;charset=UTF-8"); 
String path = request.getSession().getServletContext().getRealPath("/");
AiyoFileUpload aiyo=new AiyoFileUpload();
//获取图片
List<MultipartFile> files = fileRequest.getFiles("multiupfile");   // 上传的图集
System.out.println("图片个数:                        "+files.size());
String NewUpfilePath = path+"upfile";

JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
for (MultipartFile multipartFile : files) {
CommonsMultipartFile uploadfile = (CommonsMultipartFile)multipartFile;
System.out.println("uploadfile.getOriginalFilename()     "+uploadfile.getOriginalFilename());
System.out.println("uploadfile.isEmpty()   "+uploadfile.isEmpty());
System.out.println("uploadfile.getSize()   "+uploadfile.getSize());
String uploadfilename = aiyo.fileupload(uploadfile, NewUpfilePath);
System.out.println("文件名:                                "+uploadfilename);
String uploadfilesaveurl = request.getContextPath() + "/upfile/";
String uploadfileurl = uploadfilesaveurl+uploadfilename;  
System.out.println(uploadfileurl);
jsonObject.put("url", uploadfileurl);
jsonArray.add(jsonObject);
}
System.out.println("url链接json:                                          "+jsonArray.toString());
response.getWriter().print("<script>parent.UpdateMsg('" + jsonArray.toString() + "')</script>");
//response.getWriter().print("<script>window.οnlοad=UpdateMsg('" + jsonArray.toString() + "')</script>");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值