AJAX file upload tutorial
Home - Tutorials - AJAX basic tutorials
In this tutorial I will show you how to create simple AJAX file upload system using PHP and JavaScript.
Tutorial info:
Name: | AJAX file upload tutorial |
Total steps: | 1 |
Category: | AJAX basic tutorials |
Date: | 2008-02-25 |
Level: | Normal |
Product: | See complete product |
Viewed: | 1025782 |
Bookmark AJAX file upload tutorial
Step 1 - AJAX file upload
AJAX file upload tutorial
First of all I have to say that to create a pure AJAX file upload system is not possible because of security limitations of JavaScript. All of the Ajax upload systems I know use some third party tool/package or only mimics the AJAX feeling. Even so it makes file upload process a bit nicer. In the next section I will present you a solution which imitates the AJAX process, but uses a normal upload process and iFrames.
The concept:
- Create a simple HTML form for file upload
- Set the target to an iFrame which is on the actual page but not visible
- Call a JavaScript function on form submit to display the animation
- After the PHP part finishes the upload process, then it hides the animation
Creating the HTML file:
The HTML file we will use in this article is quite simple. It just have a simple form with a file field and a submit button. However don't forget about the enctype parameter.
Code:
Besides this we need to add a bit more code to it. First we need a block where we will display the progress animation. We need an other block where we inform the visitor whether the upload was success or not. Besides this we need to add a hidden iFrame to the page which is used as the form target. At least we add an onSubmit event to the form. So our HTML body looks like this:
Code:
<p id="result"></p> <form action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" > File: <input name="myfile" type="file" /> <input type="submit" name="submitBtn" value="Upload" /> </form> <iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe> </body>
By default the progress animation block content is hidden. We need a JavaScript function which makes this block visible if the submit button was pressed. It is a very simple code, that only changes the visibility parameter.
Code:
function startUpload(){ document.getElementById('f1_upload_process').style.visibility = 'visible'; return true; }
Besides this we need an other function, what we will call at the end of the upload process. This code will be print out a result message depending on it's parameter and hides the progress block again.
Code:
function stopUpload(success){ var result = ''; if (success == 1){ document.getElementById('result').innerHTML = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>'; } else { document.getElementById('result').innerHTML = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>'; } document.getElementById('f1_upload_process').style.visibility = 'hidden'; return true; }
Before we creating the PHP code we still need to create a style for the form and the progress block. The form style is very simple, but the progress block style sets the z-index, position and visibility parameter as well.
Code:
#f1_upload_process{ z-index:100; position:absolute; visibility:hidden; text-align:center; width:400px; margin:0px; padding:0px; background-color:#fff; border:1px solid #ccc; } form{ text-align:center; width:390px; margin:0px; padding:5px; background-color:#fff; border:1px solid #ccc; }
Now we can focus on the server side.
Server side code:
The server side code is written in PHP and it is very short and simple. First we need to set the file destination path. In this case we will use the actual working directory. Afterwards we introduce a variable that shows if there was an error or not during the upload process. Next we move the uploaded file from the tmp directory to it's defined location and set the result variable if upload was success. At the end I have inserted a sleep command to make animation a bit longer in case of very fast uploads.
Now the end the PHP code looks like this:
Code:
<?php $result = 0; $result = 1; } ?>
Finish the work from iFrame:
The output of the PHP code will be displayed / executed inside the iFrame. As you remember the iFrame is not visible, however we can call a JavaScript function here. And exactly this is the point where we can call the JavaScript function defined in the HTML code to hide the progress animation and display the file upload result on the main page. We can do it with the following JavaScript code:
Code:
<script language="javascript" type="text/javascript"> window.top.window.stopUpload(<?php echo $result; ?>); </script>
That's it. You can find a complete AJAX file upload system in the Products section.
Tags: ajax file upload tutorial, ajax file upload, file upload tutorial, ajax file, ajax upload
AJAX file upload tutorial - Table of contents |
---|
Step 1 - AJAX file upload |