javascript模拟简单的文件上传域

Js代码   收藏代码
  1. /** 
  2.  * @author wsf (javascript模拟文件上传域) 
  3.  */  
  4. function createFileIpt(container, name) {  
  5.     var filhtml = [];  
  6.     filhtml.push("<div class='file-box'>");  
  7.     filhtml.push("<input type='text' name='textfield' id='textfield' class='txt' />");  
  8.     filhtml.push("<input type='button' class='btn' value='浏览...' />");  
  9.     filhtml.push("<input type='file' name='"  
  10.                     + name  
  11.                     + "' class='file' id='"  
  12.                     + name  
  13.                     + "' size='28' οnchange=\"document.getElementById('textfield').value=this.value\"/>");  
  14.     filhtml.push("</div>");  
  15.     container.append(filhtml.join(""));  
  16.     appendCss();  
  17. }  
  18. /** 
  19.  * 添加样式 
  20.  * @returns 
  21.  */  
  22. function appendCss() {  
  23.     $(".file-box").css({  
  24.         "position" : "relative",  
  25.         "width" : "100%",  
  26.         "cursor":"pointer"  
  27.     });  
  28.     $(".txt").css({  
  29.         "height" : "22px",  
  30.         "border" : "1px solid #cdcdcd",  
  31.         "width" : "180px",  
  32.         "cursor":"pointer"  
  33.     });  
  34.     $(".btn").css({  
  35.         "background-color" : "#FFF",  
  36.         "border" : "1px solid #CDCDCD",  
  37.         "height" : "24px",  
  38.         "width" : "70px",  
  39.         "cursor":"pointer"  
  40.     });  
  41.     var _pos = $(".txt").position();  
  42.     $(".file").css({  
  43.         "position" : "absolute",  
  44.         "top" : "0",  
  45.         "left" : _pos.left + "px",  
  46.         "height" : "24px",  
  47.         "filter" : "alpha(opacity:0)",  
  48.         "opacity" : "0",  
  49.         "width" : "250px",  
  50.         "cursor":"pointer"  
  51.     });  
  52. }  
  53. /** 
  54.  * 创建 
  55.  */  
  56. createFileIpt($("#filecontainer"), "logo");  
  57. /** 
  58.  * ajax上传文件 
  59.  * @returns {Boolean} 
  60.  */  
  61. function ajaxFileUpload() {  
  62.     $.ajaxFileUpload({  
  63.         url : 'some.do',//用于文件上传的服务器端请求地址  
  64.         secureuri : false,//一般设置为false  
  65.         fileElementId : 'logo',//文件上传空间的id属性  <input type="file" id="file" name="file" />  
  66.         dataType : 'json',//返回值类型 一般设置为json  
  67.         success : function(data, status)//服务器成功响应处理函数  
  68.         {  
  69.             alert(data.message);//从服务器返回的json中取出message中的数据,其中message为在struts2中action中定义的成员变量  
  70.             if (typeof (data.error) != 'undefined') {  
  71.                 if (data.error != '') {  
  72.                     alert(data.error);  
  73.                 } else {  
  74.                     alert(data.message);  
  75.                 }  
  76.             }  
  77.         },  
  78.         error : function(data, status, e)//服务器响应失败处理函数  
  79.         {  
  80.             alert(e);  
  81.         }  
  82.     })  
  83.     return false;  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值