We use AJAX whenever we need to communicate with the server, without reloading the page. But this doesn’t function in the case of File Upload. In usual view, uploading files with AJAX is impossible! (FireFox/Mozilla can do this after change a setting in “about:config” – which we cannot guarantee for a lot of users).
When I was searching for any technique to upload file without reloading page, I found a script in www.webtoolkit.info . But it seemed very complex to me. Then I try to simplify their technique and its working great. So I thought why not share the technique with everybody?
First, look at the demo here to see how it works.
OK. Let’s get into the deep. What happens when we submit a form? The form is submitted to the action page and that page loaded in current frame of window. If the “target” attribute of form indicate “_blank” or any frame name of the window, the page loads in a blank window or in the named frame or inner frame. Here we’ll be using the second method.
First, take an inner frame in anywhere of the page. And set the display property of this frame as “none”:
<iframe name="hiddenFrame" id="hiddenFrame"
src="about:blank" style="display:none" >
</iframe>
Now, just set this frame as the target of your uploading form.
<form name="testForm" id="testForm" enctype="multipart/form-data"
action="upload.php" method="post" target="hiddenFrame">
<input type="file" name="name" />
<input type="submit" value="Upload!" />
</form>
If we submit this form now, it will submit the file to the target script on the server (can be in php, asp etc.). The script will upload the file and will load the response in the frame “hiddenFrame”. As the frame is not visible, nothing about this loading will be seen by the visitors. Now, as the uploading page displays nothing after uploading your file, the response can be immediate. Although it’s not AJAX, this technique will upload your file without (visually) reloading page.
What more?
For many purpose, we might need a callback function when using AJAX. For this file uploading technique also, we will try to create something like that. First, we’ll write the callback function as a simple JavaScript function in the header of the uploading page:
<script language="javascript">
function trackThis()
{
var resultDiv =
window.frames.hiddenFrame.document.getElementById(‘result’);
var uploadedImage = document.getElementById(‘UploadedImage’);
uploadedImage.src = resultDiv.innerHTML;
uploadedImage.style.display="block";
alert("File Uploaded :" + resultDiv.innerHTML);
}
</script>
When the uploading of the server page is complete, it should print the JavaScript code in the hidden iframe, to call the callback function with the reference “window.top”. Any other required information also can be printed in specific DIVs. It will enable us to retrieve any response information using the innerHTML of any specific DIV (as shown in example callback function).
Example:
<?php
print_r($_FILES["name"]);
if(move_uploaded_file($_FILES["name"]["tmp_name"],
dirname(__FILE__)."/".$_FILES['name']['name']))
{
echo "Your uploaded file is: <div id=’result’>".$_FILES['name']['name']." </div>";
}
else
{
echo "<div id=’result’>Sorry! Error occured!</div>";
}
echo <<<test
<script type="text/javascript">
window.top.trackThis()
</script>
test;
?>
Well, that’s it. This technique has already saved my ass once, see if it can save yours also when you need.







Cool. This is heading to be a resourceful blog - where I might come to see if any of my problems being solved once.
Great job anis bhai! Keep this blogging spirit up and soon the world will recognize your potential.
Cheers
Wao!!!!!!
it’s a great work. Well done anis bhai.. Keep it up.
its not excellent
it not very good
it not good
it is very bed
Because I don’t know php, so i cant understand it.
But i think it will super duper for them, who will understand it.
It’s really helpful. Thanks to u for this article and wants more …
Thanks
Valentica
mm hmm - great article.
did u check swfUpload object?
Anis vai,
I’ve tried to use this technique in code igniter but failed.
Would u plz help me?
Thanks!
@Jahed
Sure.
Mr. Kabir of Right Brain Solution Ltd. already working with this technique successfully in codeigniter. You can contact with him or send me your code how u have tried. I will try to solve it.
Thanx
Anis uddin Ahmad
Oh !
You r realy a boss of the bosses. This article realy helped me. I’ve used it in code igniter and it works well.
Go On ….
Thanks!
great work bondhu !!!!!!!!!!
real bondhu………………..
Hi
First, I have used this and it works great for my needs.
However remember that when you load content into an iframe, it also adds an entry into the browser’s history. Therefore after the file upload (or worse after 5 subsequent uploads) the history contains 5 entries relating to the iframe, which may confuse the user as they are clicking back back back and nothing (in their view) is happening because the iframe is loading its previous pages, and you can’t see them.
That is unless I have got the wrong end of the stick.
Hi James,
Thanx a lot for discovering the nice problem.
It has an easy solution. That is, after uploading the file, in the callback function, just turn back the browser history by “history.back()” method of javascript.
This will not change anything visually to the user. Because, the change is happening in the invisible iframe only.
Thanx again James.
Hi,
I am doing something similar, but am having a large problem. Every time I upload a file, I get a ‘ghost’ entry in my browser history (i.e. an extra entry in the ‘back’ button).
How can I keep this from happening? If I create a dynamic iframe, I know it won’t show, but then I can’t use it as a target for a form…
Any suggestions?
Hi brad,
Thanx for ur asking.
It’s not ghost entry. Every page loading, (even in frameset) added in the browser history. See the solution in 12th comment.
Thanx again brad.
Hi, I really was looking for the technique to develope such script. It really helps me alot. Thanks for this article.
I use the same technique but different code to upload files to a server. In my version however Firefox keeps saying the page is loading, even after the response is back, processed and the iframe removed from the page.
Sound familiar, any solution?
Solved. The problem was that I process a callback function from the iframe when it finishes loading with the onload event. At the end of the call back function i remove the iframe object from its parent, resulting in a never completely loaded page.
The solution is simple: I’ve added a settimeout as a last line to delegate the removing of the frame as a last line to the callback structure - now the page finishes loading and gets removed immediately after.
Just thought I’d mention it - I’ve seen similar questions without any answer on several websites.
Yes,
It works thanks a lot
Dear Ahmed,
Thanks for this simplicity. The truth is that I had to use some of my brain (which I hate to do), however it was worth of it.
I am developing an intranet portal in Joomla CMS this script is core part of one of modules that I had to do.
Greetings from Serbia!!!