Solved

I created an ajax file upload with progress bar. Now I need a cancel button.

Posted on 2014-07-25
2
805 Views
Last Modified: 2014-07-25
I currently have a working FLEX/Actionscript solution with progress bar and cancel button, but it's a little bulky and time consuming to trouble shoot. I created an AJAX/PHP video file upload yesterday and it works nicely.

I need a way to cancel an (ajax-php) video file upload. Can I just use the javaScript function abort() and then redirect the user to a different page or will that leave the server and php file in a messy situation with a half uploaded file? Any advise would be appreciated. Thanks.

I posted just some of the code.
ajax.upload.addEventListener('progress', progressHandler, false);
ajax.addEventListener('load', completeHandler, false);
ajax.addEventListener('error', errorHandler, false);
ajax.addEventListener('abort', abortHandler, false);
ajax.open('post', '/upload_file.php', true);

function abortHandler(event) {
      document.getElementById('element-info').innerHTML = 'Upload canceled';
}

<?php
$filename = $_FILES['file']['name'];
$fileTmpLoc = $_FILES['file']['tmp_name'];
$fileType = $_FILES['file']['type'];
$fileSize = $_FILES['file']['size'];
$fileErroeMsg = $_FILES['file']['error'];

if(!$fileTmpLoc) {'Please browse for a file before clicking the arrow button.'; exit;}

$success = move_uploaded_file($fileTmpLoc, $filename);

if ($success) {
      echo 'upload complete.';
} else {
            echo 'try again';
      }

?>
0
Comment
Question by:kadin
2 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
Comment Utility
Use .abort() on the ajax object
Normally you don't need to do anything as the PHP script will abort if it fails to send something to the browser
0
 

Author Comment

by:kadin
Comment Utility
Thanks. I will give it a try.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now