Solved

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

Posted on 2014-07-25
2
833 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
ID: 40220625
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
ID: 40220645
Thanks. I will give it a try.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

803 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