Hopes

Start Here..

 

上传文件

Html代码  收藏代码
  1. <html>      
  2.      
  3. <head>      
  4.      
  5. <title>Add Files</title>      
  6.      
  7. <style>      
  8.      
  9. a.addfile {      
  10.      
  11. background-image:url(http://p.mail.163.com/js31style/lib/0703131650/163blue/f1.gif);      
  12.      
  13. background-repeat:no-repeat;      
  14.      
  15. background-position:-823px -17px;      
  16.      
  17. display:block;      
  18.      
  19. float:left;      
  20.      
  21. height:20px;      
  22.      
  23. margin-top:-1px;      
  24.      
  25. position:relative;      
  26.      
  27. text-decoration:none;      
  28.      
  29. top:0pt;      
  30.      
  31. width:80px;      
  32.      
  33. }      
  34.      
  35.      
  36.      
  37. input.addfile {      
  38.      
  39. /*left:-18px;*/     
  40.      
  41. }      
  42.      
  43.      
  44.      
  45. input.addfile {      
  46.      
  47. cursor:pointer !important;      
  48.      
  49. height:18px;      
  50.      
  51. left:-13px;      
  52.      
  53. filter:alpha(opacity=0);      
  54.      
  55. position:absolute;      
  56.      
  57. top:5px;      
  58.      
  59. width:1px;      
  60.      
  61. z-index: -1;      
  62.      
  63. }      
  64.      
  65. </style>      
  66.      
  67.      
  68.      
  69. <script type="text/javascript">      
  70.      
  71.      
  72.      
  73. function MultiSelector(list_target, max)      
  74.      
  75. {      
  76.      
  77.     // Where to write the list      
  78.      
  79.     this.list_target = list_target;      
  80.      
  81.     // How many elements?      
  82.      
  83.     this.count = 0;      
  84.      
  85.     // How many elements?      
  86.      
  87.     this.id = 0;      
  88.      
  89.     // Is there a maximum?      
  90.      
  91.     if (max)      
  92.      
  93.      {      
  94.      
  95.         this.max = max;      
  96.      
  97.      }      
  98.      
  99.     else      
  100.      
  101.      {      
  102.      
  103.         this.max = -1;      
  104.      
  105.      };      
  106.      
  107.      
  108.      
  109.     /**   
  110.   
  111.       * Add a new file input element   
  112.   
  113.       */     
  114.      
  115.     this.addElement = function(element)      
  116.      
  117.      {      
  118.      
  119.         // Make sure it's a file input element      
  120.      
  121.         if (element.tagName == 'INPUT' && element.type == 'file')      
  122.      
  123.          {      
  124.      
  125.             // Element name -- what number am I?      
  126.      
  127.              element.name = 'file_' + this.id++;      
  128.      
  129.      
  130.      
  131.             // Add reference to this object      
  132.      
  133.              element.multi_selector = this;      
  134.      
  135.      
  136.      
  137.             // What to do when a file is selected      
  138.      
  139.              element.onchange = function()      
  140.      
  141.              {      
  142.      
  143.                 // New file input      
  144.      
  145.                 var new_element = document.createElement('input');      
  146.      
  147.                  new_element.type = 'file';      
  148.      
  149.                  new_element.size = 1;      
  150.      
  151.                  new_element.className = "addfile";      
  152.      
  153.      
  154.      
  155.                 // Add new element      
  156.      
  157.                 this.parentNode.insertBefore(new_element, this);      
  158.      
  159.      
  160.      
  161.                 // Apply 'update' to element      
  162.      
  163.                 this.multi_selector.addElement(new_element);      
  164.      
  165.      
  166.      
  167.                 // Update list      
  168.      
  169.                 this.multi_selector.addListRow(this);      
  170.      
  171.      
  172.      
  173.                 // Hide this: we can't use display:none because Safari doesn't like it      
  174.      
  175.                 this.style.position = 'absolute';      
  176.      
  177.                 this.style.left = '-1000px';      
  178.      
  179.              };      
  180.      
  181.      
  182.      
  183.      
  184.      
  185.             // If we've reached maximum number, disable input element      
  186.      
  187.             if (this.max != -1 && this.count >= this.max)      
  188.      
  189.              {      
  190.      
  191.                  element.disabled = true;      
  192.      
  193.              };      
  194.      
  195.      
  196.      
  197.             // File element counter      
  198.      
  199.             this.count++;      
  200.      
  201.             // Most recent element      
  202.      
  203.             this.current_element = element;      
  204.      
  205.          }      
  206.      
  207.         else      
  208.      
  209.          {      
  210.      
  211.             // This can only be applied to file input elements!      
  212.      
  213.              alert('Error: not a file input element');      
  214.      
  215.          };      
  216.      
  217.      };      
  218.      
  219.      
  220.      
  221.      
  222.      
  223.     /**   
  224.   
  225.       * Add a new row to the list of files   
  226.   
  227.       */     
  228.      
  229.     this.addListRow = function(element)      
  230.      
  231.      {      
  232.      
  233.         // Row div      
  234.      
  235.         var new_row = document.createElement('div');      
  236.      
  237.      
  238.      
  239.         // Delete button      
  240.      
  241.         var new_row_button = document.createElement('input');      
  242.      
  243.          new_row_button.type = 'button';      
  244.      
  245.          new_row_button.value = 'Delete';      
  246.      
  247.      
  248.      
  249.         // References      
  250.      
  251.          new_row.element = element;      
  252.      
  253.      
  254.      
  255.         // Delete function      
  256.      
  257.          new_row_button.onclick = function()      
  258.      
  259.          {      
  260.      
  261.             // Remove element from form      
  262.      
  263.             this.parentNode.element.parentNode.removeChild(this.parentNode.element);      
  264.      
  265.      
  266.      
  267.             // Remove this row from the list      
  268.      
  269.             this.parentNode.parentNode.removeChild(this.parentNode);      
  270.      
  271.      
  272.      
  273.             // Decrement counter      
  274.      
  275.             this.parentNode.element.multi_selector.count--;      
  276.      
  277.      
  278.      
  279.             // Re-enable input element (if it's disabled)      
  280.      
  281.             this.parentNode.element.multi_selector.current_element.disabled = false;      
  282.      
  283.      
  284.      
  285.             // Appease Safari      
  286.      
  287.             // without it Safari wants to reload the browser window      
  288.      
  289.             // which nixes your already queued uploads      
  290.      
  291.             return false;      
  292.      
  293.          };      
  294.      
  295.      
  296.      
  297.         // Set row value      
  298.      
  299.          new_row.innerHTML = element.value + " ";      
  300.      
  301.      
  302.      
  303.         // Add button      
  304.      
  305.          new_row.appendChild(new_row_button);      
  306.      
  307.      
  308.      
  309.         // Add it to the list      
  310.      
  311.         this.list_target.appendChild(new_row);      
  312.      
  313.      };      
  314.      
  315. };      
  316.      
  317. </script>      
  318.      
  319. </head>      
  320.      
  321.      
  322.      
  323. <body>      
  324.      
  325.      
  326.      
  327. <!-- This is the form -->      
  328.      
  329. <form enctype="multipart/form-data" action="http://127.0.0.1:8080/xxx/fileUploadAction.action" method="post">      
  330.      
  331. <!-- The file element -- NOTE: it has an ID -->      
  332.      
  333. <a href="javascript:void(1==1);" class="addfile" style="cursor: default;" hidefocus="true">      
  334.      
  335. <input id="my_file_element" class="addfile" type="file" name="file_1" size="1" title="点击选择附件">      
  336.      
  337. </a>      
  338.      
  339. <input type="submit" value="上 传">      
  340.      
  341. </form>      
  342.      
  343.      
  344.      
  345. Files:      
  346.      
  347. <!-- This is where the output will appear -->      
  348.      
  349. <div id="files_list" style="padding:5px;border:1px;border-style:solid;border-color:#0000ff;height:100px;width:600px;"></div>      
  350.      
  351. <script>      
  352.      
  353. <!-- Create an instance of the multiSelector class, pass it the output target and the max number of files -->      
  354.      
  355. var multi_selector = new MultiSelector(document.getElementById('files_list'), 100);      
  356.      
  357. <!-- Pass in the file element -->      
  358.      
  359. multi_selector.addElement(document.getElementById('my_file_element'));      
  360.      
  361. </script>      
  362.      
  363.      
  364.      
  365. </body>      
  366.      
  367. </html>     












美化上传控件的办法;

2007-04-27 16:39 by 迷路中的路人甲, 364 阅读, 0 评论, 收藏编辑
还有一个问题就是,如何利用这一个控件,进行多个文档的上传工作?
    利用dom操作,当每次选择了一个文件的时候,隐藏该file域,在相同位置创建一个新的供下次点击,删除的时候只要直接删掉隐藏的就行了[初步想法未经证实]
一直以来上传控件input file都无法进行美化,例如换个图片什么的;查了很多资料最终就是需要利用隐藏file域实现上传功能;其实很简单,从163里面找了一个样式出来,搞定;

js代码: //依赖prototype.js
function selfile()
{
  $(
"fileurl").value = $("file").value;
}   

css代码://来自mail.163.com
a.addfile{width:70px;height:20px;position:relative;cursor:hand;top:4px;top/**/:0;text-decoration:none;background-position:-823px -17px;display:inline;float:left;margin-top:-5px;margin-top/**/:-1px}
*:lang(zh) a.addfile
{margin-top:-2px;cursor:pointer}
a.addfile:hover
{background-position:-911px -17px;text-decoration:none}
input.addfile
{width:1px;height:18px;cursor:pointer!important;cursor:hand;position:absolute;top:5px;left:-5px;left/**/:-3px;opacity:0;filter:alpha(opacity=0)}
*:lang(zh) input.addfile
{left:-18px}

html代码://
<input type="text" value="" id="fileurl" name="fileurl" readonly/>
<a  href="#" class="addfile"> <input type="file" name="file" id="file" hideFocus class="addfile" onChange="selfile();"/>+添加附件(也可以放个美化的图片) </a>
















posted on 2012-07-19 11:06 ** 阅读(162) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

公告

你好!

常用链接

留言簿(2)

随笔档案

文章分类

文章档案

新闻档案

相册

收藏夹

C#学习

友情链接

搜索

最新评论

阅读排行榜

评论排行榜