找回密码
 注册账户
查看: 2030|回复: 3

ajax upload - Like AJAX

[复制链接]
棋子 发表于 2007-7-22 15:02:35 | 显示全部楼层 |阅读模式
ajax upload - Like AJAX

1) Put the code above in a file called micoxUpload.js

  1. /* standard small functions */
  2. function $m(quem){
  3. return document.getElementById(quem)
  4. }
  5. function remove(quem){
  6. quem.parentNode.removeChild(quem);
  7. }
  8. function addEvent(obj, evType, fn){
  9. // elcio.com.br/crossbrowser
  10.     if (obj.addEventListener)
  11.         obj.addEventListener(evType, fn, true)
  12.     if (obj.attachEvent)
  13.         obj.attachEvent("on"+evType, fn)
  14. }
  15. function removeEvent( obj, type, fn ) {
  16.   if ( obj.detachEvent ) {
  17.     obj.detachEvent( 'on'+type, fn );
  18.   } else {
  19.     obj.removeEventListener( type, fn, false ); }
  20. }
  21. /* THE UPLOAD FUNCTION */
  22. function micoxUpload(form,url_action,id_element,html_show_loading,html_error_http){
  23. /******
  24. * micoxUpload - Submit a form to hidden iframe. Can be used to upload
  25. * Use but dont remove my name. Creative Commons.
  26. * Versão: 1.0 - 03/03/2007 - Tested no FF2.0 IE6.0 e OP9.1
  27. * Author: Micox - Náiron JCG - elmicoxcodes.blogspot.com - [email][email protected][/email]
  28. * Parametros:
  29. * form - the form to submit or the ID
  30. * url_action - url to submit the form. like action parameter of forms.
  31. * id_element - element that will receive return of upload.
  32. * html_show_loading - Text (or image) that will be show while loading
  33. * html_error_http - Text (or image) that will be show if HTTP error.
  34. *******/

  35. //testing if 'form' is a html object or a id string
  36. form = typeof(form)=="string"?$m(form):form;

  37. var erro="";
  38. if(form==null || typeof(form)=="undefined"){ erro += "The form of 1st parameter does not exists.\n";}
  39. else if(form.nodeName.toLowerCase()!="form"){ erro += "The form of 1st parameter its not a form.\n";}
  40. if($m(id_element)==null){ erro += "The element of 3rd parameter does not exists.\n";}
  41. if(erro.length>0) {
  42.   alert("Error in call micoxUpload:\n" + erro);
  43.   return;
  44. }

  45. //creating the iframe
  46. var iframe = document.createElement("iframe");
  47. iframe.setAttribute("id","micox-temp");
  48. iframe.setAttribute("name","micox-temp");
  49. iframe.setAttribute("width","0");
  50. iframe.setAttribute("height","0");
  51. iframe.setAttribute("border","0");
  52. iframe.setAttribute("style","width: 0; height: 0; border: none;");

  53. //add to document
  54. form.parentNode.appendChild(iframe);
  55. window.frames['micox-temp'].name="micox-temp"; //ie sucks

  56. //add event
  57. var carregou = function() {
  58.    removeEvent( $m('micox-temp'),"load", carregou);
  59.    var cross = "javascript: ";
  60.    cross += "window.parent.$m('" + id_element + "').innerHTML = document.body.innerHTML; void(0); ";
  61.    
  62.    $m(id_element).innerHTML = html_error_http;
  63.    $m('micox-temp').src = cross;
  64.    //del the iframe
  65.    setTimeout(function(){ remove($m('micox-temp'))}, 250);
  66.   }
  67. addEvent( $m('micox-temp'),"load", carregou)

  68. //properties of form
  69. form.setAttribute("target","micox-temp");
  70. form.setAttribute("action",url_action);
  71. form.setAttribute("method","post");
  72. form.setAttribute("enctype","multipart/form-data");
  73. form.setAttribute("encoding","multipart/form-data");
  74. //submit
  75. form.submit();

  76. //while loading
  77. if(html_show_loading.length > 0){
  78.   $m(id_element).innerHTML = html_show_loading;
  79. }

  80. }
复制代码


2) Include this file in your HTML
  1. <script type="text/javascript" src="micoxUpload.js"></script>
复制代码


3) Parameters of micoxUpload function:

   1. form - the form to submit or the ID of a form .
   2. url_action - url to submit the form. like 'action' parameter of forms.
   3. id_element - element that will receive return of upload.
   4. html_show_loading - Text (or image) that will be show while loading
   5. html_error_http - Text (or image) that will be show if HTTP error.

4) Ok. Now you have a lot of forms to call the Asynchronous upload function. 3 Examples:

  1. 4.1) Basic from a button (or input-type-button) in a form:

  2. [code]<legend>Basic use</legend>
  3.   <form>
  4.     <input type="file" name="name" />
  5.     <div id="upload_1" class="recebe">&nbsp;</div>
  6.     <button 'upa.php','upload_1','Loading...','Error in upload'); return false;" type="button">test</button>
  7.   </form>
  8. </fieldset>
复制代码


4.2) When input-file lost focus (onblur):

<fieldset>
<legend>On blur use</legend>
  <form>
    <input type="file" name="name" 'upa.php','upload_2','Loading...','Error in upload')" />
    <div id="upload_2" class="recebe">&nbsp;</div>
  </form>
</fieldset>

4.3) Making acessible whitout JavaScript :

<fieldset>
<legend>Unobstrusive</legend>
    <form action="upa.php" target="_blank">
    <input type="file" name="name" 'upa.php','upload_3','Loading...','Error in upload')" />
    <div id="upload_3" class="recebe">&nbsp;</div>
    <button type="submit">Go</button>
  </form>
</fieldset>
[/code]
Ok. Bugs, errors, comment here.
In next post, a more flexible version of this function for who understend JavaScript.
laokun 发表于 2008-4-7 22:48:30 | 显示全部楼层
Helpful...Thx
似的热突然 发表于 2008-8-26 08:00:01 | 显示全部楼层
头像被屏蔽
7748533 发表于 2008-9-21 02:55:27 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册账户

本版积分规则

存档|黑屋|手机|网络实验室 本站服务器由美国合租以及IDCLayer国际数据提供!!!

GMT+8, 2026-6-9 08:42 , Processed in 0.013772 second(s), 9 queries , Gzip On, Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表