Reversing an upload in AJAX

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gearoid
    New Member
    • Mar 2008
    • 21

    Reversing an upload in AJAX

    Hey

    I've designed an intranet site where users can upload documents (called 'resources') as part of a knowledge base. Part of the functionality of said site is that privilaged users can edit the documents in the system. This involves uploading a new file for a 'resource'. However, after a user has performed their editing, they are asked to confirm their changes (in a page called confirm.php). When confirm.php loads, the new file upload is performed but the neccessary changes to the database are not performed until the user clicks 'Confirm' (which brings them to update.php). This works fine so long as the user validates their changes on confirm.php - but if the user clicks 'Undo' on confirm.php, the uploaded document stays on the network (although it is not connected to any resource) and the user is directed back to editing the Resource.

    I wish to design an AJAX function that will receive the uploaded files network path. I believe that this should be as simple as attaching an onclick command to the undo button, like so:


    [HTML]<INPUT TYPE='BUTTON' VALUE='Undo' onClick='revers eUpload($upload edFileTargetPat h);'>[/HTML]

    and then writing the AJAX function which will pass this target path into a php file called something like 'cancelUpload.p hp' which will be able to unlink the uploaded file. So far I have used the following AJAX code with no joy:

    Code:
    <script language="javascript" type="text/javascript">
    <!--
    //Browser Support Code
    
    function reverseUpload(filePath){
            var ajaxRequest;  // The variable that makes Ajax possible!
    
            try{
                    // Opera 8.0+, Firefox, Safari
                    ajaxRequest = new XMLHttpRequest();
            } catch (e){
                    // Internet Explorer Browsers
                    try{
                            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                    } catch (e) {
                            try{
                                    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                            } catch (e){
                                    // Something went wrong
                                    alert("Your browser broke!");
                                    return false;
                            }
                    }
            }
            // Create a function that will receive data sent from the server
            ajaxRequest.onreadystatechange = function(){
                    if(ajaxRequest.readyState == 4){
                            
                    }
            }
            ajaxRequest.open("GET", "cancelUpload.php?path="+filePath, true);
            ajaxRequest.send(null);
    }
    
    //-->
    </script>
    and the cancelUpload.ph p file looks like this:


    [PHP]$path = $_GET['targetPath'];

    unlink($path);

    ECHO "blah";[/PHP]


    Any help would be greatly appreciated. Thanks!
  • sivakumarsubash
    New Member
    • Mar 2008
    • 2

    #2
    sorry i dont have any idea

    Comment

    • gearoid
      New Member
      • Mar 2008
      • 21

      #3
      Anybody have any suggestions?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by sivakumarsubash
        sorry i dont have any idea
        If you didn't have any idea, there was no need to tell the world that refreshing pearl of wisdom. By answering, you removed this thread from the unanswered list thereby giving it less chance of receiving attention.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Originally posted by gearoid
          I've designed an intranet site where users can upload documents (called 'resources') as part of a knowledge base. Part of the functionality of said site is that privilaged users can edit the documents in the system. This involves uploading a new file for a 'resource'. However, after a user has performed their editing, they are asked to confirm their changes (in a page called confirm.php). When confirm.php loads, the new file upload is performed but the neccessary changes to the database are not performed until the user clicks 'Confirm' (which brings them to update.php). This works fine so long as the user validates their changes on confirm.php - but if the user clicks 'Undo' on confirm.php, the uploaded document stays on the network (although it is not connected to any resource) and the user is directed back to editing the Resource.
          Why not just delete the document using the Undo button in confirm.php?

          Just a note: you should use the POST method when making changes on the server.

          Comment

          • gearoid
            New Member
            • Mar 2008
            • 21

            #6
            Originally posted by acoder
            Why not just delete the document using the Undo button in confirm.php?

            Just a note: you should use the POST method when making changes on the server.
            Thanks for the tip.

            When you say "delete the document using the Undo button" do you mean: create another form on the page, have the target file as a hidden field and then have a Submit button (labelled 'Undo') call a page which unlinks the file?

            Seems like a much simpler solution than what I'd planned - thanks a lot!

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Originally posted by gearoid
              When you say "delete the document using the Undo button" do you mean: create another form on the page, have the target file as a hidden field and then have a Submit button (labelled 'Undo') call a page which unlinks the file?

              Seems like a much simpler solution than what I'd planned - thanks a lot!
              That sounds about right. Let us know if you have any problems with it.

              Comment

              • gearoid
                New Member
                • Mar 2008
                • 21

                #8
                Originally posted by acoder
                That sounds about right. Let us know if you have any problems with it.
                Working perfectly. Thanks again!

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  You're welcome. Glad you got it working!

                  Comment

                  Working...