SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Mazel tov! bronze trophy kohoutek's Avatar
    Join Date
    Aug 2004
    Location
    Hamburg, Germany
    Posts
    4,248
    Mentioned
    30 Post(s)
    Tagged
    0 Thread(s)

    Question How to upload multiple files via Ajax?

    Hello again,

    I would like to upload multiple files via Ajax. Right now, if I add a file via Ajax, all is good. The file stays there as it should. Once I click a new link, the previous request disappears, hence the browser always shows the current selection. How can I make it so that ALL files that have been clicked on remain in the browser?

    Thanks in advance,

    kohoutek

    This is the javascript:

    Code JavaScript:
    function getHTTPObject() {
      var xhr = false;
      if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        try {
          xhr = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
          try {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
          } catch(e) {
            xhr = false;
          }
        }
      }
      return xhr;
    }
     
    function grabFile(file) {
      var request = getHTTPObject();
      if (request) {
        request.onreadystatechange = function() {
          parseResponse(request);
        };
        request.open("GET", file, true);
        request.send(null);
     
      }
     
    }
     
    function parseResponse(request) {
      if (request.readyState == 4) {
        if (request.status == 200 || request.status == 304) {
          var data = request.responseXML;
          var myitem = data.getElementsByTagName("product")[0].firstChild.nodeValue;
          var name = data.getElementsByTagName("name")[0].firstChild.nodeValue;
          var image = data.getElementsByTagName("image")[0].firstChild.nodeValue;
          var header = document.createElement("h2");
          var para = document.createElement("p");
          var text = document.createTextNode(name);
          para.appendChild(text); 
          var productImage = document.createElement("img");
          productImage.setAttribute("src",image);
     
          var details = document.getElementById("details");
     
          while (details.hasChildNodes()) {
            details.removeChild(details.lastChild);
          }
          var remove = document.createElement("a");
          remove.setAttribute("href", "#");
          remove.appendChild(document.createTextNode(" remove"));
          remove.onclick = removemyitem;
     
          details.appendChild(header);
          details.appendChild(productImage);
          details.appendChild(para);
          details.appendChild(remove);
     
     
     
     
     
        }
      }
    }
    Maleika E. A. | Rockatee | Twitter | Dribbble



  2. #2
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    Regina, SK, Canada
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First off, you can't upload files VIA ajax. What I would suggest doing is using an iframe, it's what google does for gmail, same with hotmail. In PHP have a session_start(); on the top of the page, then once the text is dynamically added to the browser window, have ajax talk to a PHP file and add it to the session, then when you refresh the page, catch all the items in the $_SESSION[] super global and display them on the page. Example, the php page that ajax calls to would have something like $_SESSION['file'] = "myfile.zip" and then break it out in a foreach loop like so

    Code PHP:
    foreach($_SESSION as $key => $val){
      echo $_SESSION[$key].': '.$val.'<br>';
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •