943,910 Members | Top Members by Rank

Ad:
May 4th, 2009
0

Refresh after uploading is done

Expand Post »
Hi. In short, how can I make this javascript to refresh the page when uploading is done ?
javascript Syntax (Toggle Plain Text)
  1. function $m(theVar){
  2. return document.getElementById(theVar)
  3. }
  4. function remove(theVar){
  5. var theParent = theVar.parentNode;
  6. theParent.removeChild(theVar);
  7. }
  8. function addEvent(obj, evType, fn){
  9. if(obj.addEventListener)
  10. obj.addEventListener(evType, fn, true)
  11. if(obj.attachEvent)
  12. obj.attachEvent("on"+evType, fn)
  13. }
  14. function removeEvent(obj, type, fn){
  15. if(obj.detachEvent){
  16. obj.detachEvent('on'+type, fn);
  17. }else{
  18. obj.removeEventListener(type, fn, false);
  19. }
  20. }
  21. function isWebKit(){
  22. return RegExp(" AppleWebKit/").test(navigator.userAgent);
  23. }
  24. function ajaxUpload(form,url_action,id_element,html_show_loading,html_error_http){
  25. var detectWebKit = isWebKit();
  26. form = typeof(form)=="string"?$m(form):form;
  27. var erro="";
  28. if(form==null || typeof(form)=="undefined"){
  29. erro += "The form of 1st parameter does not exists.\n";
  30. }else if(form.nodeName.toLowerCase()!="form"){
  31. erro += "The form of 1st parameter its not a form.\n";
  32. }
  33.  
  34. if(erro.length>0){
  35. alert("Error in call ajaxUpload:\n" + erro);
  36. return;
  37. }
  38. var iframe = document.createElement("iframe");
  39. iframe.setAttribute("id","ajax-temp");
  40. iframe.setAttribute("name","ajax-temp");
  41. iframe.setAttribute("width","0");
  42. iframe.setAttribute("height","0");
  43. iframe.setAttribute("border","0");
  44. iframe.setAttribute("style","width: 0; height: 0; border: none;");
  45. form.parentNode.appendChild(iframe);
  46. window.frames['ajax-temp'].name="ajax-temp";
  47. var doUpload = function(){
  48. removeEvent($m('ajax-temp'),"load", doUpload);
  49. var cross = "javascript: ";
  50. cross += "window.parent.$m('"+id_element+"').innerHTML = document.body.innerHTML; void(0);";
  51. $m(id_element).innerHTML = html_error_http;
  52. $m('ajax-temp').src = cross;
  53. if(detectWebKit){
  54. remove($m('ajax-temp'));
  55. }else{
  56. setTimeout(function(){ remove($m('ajax-temp'))}, 250);
  57. }
  58. }
  59. addEvent($m('ajax-temp'),"load", doUpload);
  60. form.setAttribute("target","ajax-temp");
  61. form.setAttribute("action",url_action);
  62. form.setAttribute("method","post");
  63. form.setAttribute("enctype","multipart/form-data");
  64. form.setAttribute("encoding","multipart/form-data");
  65. if(html_show_loading.length > 0){
  66. $m(id_element).innerHTML = html_show_loading;
  67. }
  68. form.submit();
  69. }
If you need the php file:
php Syntax (Toggle Plain Text)
  1. <?php
  2. include "config.php";
  3. @header("Content-Type: text/html; charset=windows-1251");
  4. $fileN = $_REQUEST['pic'];
  5. if (isset($fileN)){
  6. $allowed_ext = "jpg,jpeg,gif,png";
  7. $filesize = $_FILES['pic']['size'];
  8. $maxlimit = 9999999999;
  9. if($filesize > 0){
  10. $filename = strtolower($_FILES['pic']['name']);
  11. $filename = preg_replace('/\s/', '_', $filename);
  12. if($filesize < 1){
  13. $errorList[] = "Empty file!";
  14. echo "Empty file!";
  15. }
  16. if($filesize > $maxlimit){
  17. $errorList[] = "File too big!";
  18. echo "File too big! (1MB max)";
  19. }
  20. if(count($errorList)<1){
  21. $file_ext = preg_split("/\./",$filename);
  22. $allowed_ext = preg_split("/\,/",$allowed_ext);
  23. $rand = rand(100,999).rand(10,99).rand(100,500);
  24. $front_name = substr($file_ext[0], 0, 15);
  25. $newfilename = $front_name."_".$rand.".".end($file_ext);
  26. foreach($allowed_ext as $ext){
  27. if($ext==end($file_ext)){
  28. $data = date("d.m.Y");
  29. $zapis = mysqlCountR("mmvet_obqvi", "date = '$data' AND userip = '$ip' AND active = 0");
  30. if($zapis > 0){
  31. $picsB = mysqlGet("mmvet_obqvi", "*", 0, "date = '$data' AND userip = '$ip' AND active = 0");
  32. $allpics = $picsB['pics'];
  33. $picsEx = explode(",", $allpics);
  34. if(sizeof($picsEx) == 5){
  35. echo "You can't upload more than 5 pictures!";
  36. }else{
  37. $plusPic = $allpics.",".$newfilename;
  38. mysqlSet("mmvet_obqvi", "pics", "$plusPic", "date = '$data' AND userip = '$ip' AND active = 0", "~ ");
  39. }
  40. }else{
  41. mysqlAdd("mmvet_obqvi", "null,null,null,null,null,null,$data,null,null,null,null,null,null,null,null,$newfilename,$ip,0");
  42. }
  43. if(sizeof($picsEx) <= 5 ){
  44. copy($_FILES['pic']['tmp_name'], "_data/uploads/".$newfilename);
  45. saveThumb("_data/uploads/".$newfilename, 283, 212, "_data/uploads/thumbbig_".$newfilename, end($file_ext));
  46. saveThumb("_data/uploads/".$newfilename, 108, 81, "_data/uploads/thumbsmall_".$newfilename, end($file_ext));
  47. unlink("_data/uploads/".$newfilename);
  48. $imgUploaded = true;;
  49. }
  50. }
  51. }
  52.  
  53. }else{
  54. $imgUploaded = false;
  55. }
  56. }else{
  57. echo "No file chosen!";
  58. $imgUploaded = false;
  59. }
  60. }else{
  61. echo "No file!";
  62. }
  63. ?>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trevata is offline Offline
9 posts
since Apr 2008
May 5th, 2009
0

Re: Refresh after uploading is done

Catch the ajax on complete state when the readyState property becomes 4, after that execute the javascript code window.location.reload() to refresh/reloading the page

Regards
Anish
Reputation Points: 10
Solved Threads: 3
Light Poster
anish.anick is offline Offline
47 posts
since Jan 2008
May 5th, 2009
0

Re: Refresh after uploading is done

Yeah. But here I don't have an XMLHTTP Object And I don't know how the javascript understands when the uploading is done
Last edited by trevata; May 5th, 2009 at 10:25 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trevata is offline Offline
9 posts
since Apr 2008
Oct 12th, 2011
0
Re: Refresh after uploading is done
I believe the whole point of this code is to avoid refreshing the page by loading the image in the hidden iframe, as you cannot really use Ajax to upload an image.

So, if you are going to refresh the page, I would use a simpler method, like sending the results of your upload form back to the top of the page and process it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
RationalRabbit is offline Offline
4 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: This is impossible (two javascripts on one page)
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Jquery coding or localhost





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC