Helo! Here a simple function to asynchronous upload using iframe. Dont reload the page. Like AJAX but its not ajax.You dont need to create the iframe, the function do this. Work like Gmail upload and Google pages upload.
Tested in Firefox 2.0, Internet Explorer 6.0 e Opera 9.1. If you test in another browsers, please write a comment.
Update: New version here (portuguese)
1) Put the code above in a file called micoxUpload.js
/* standard small functions */ function $m(quem){ return document.getElementById(quem) } function remove(quem){ quem.parentNode.removeChild(quem); } function addEvent(obj, evType, fn){ // elcio.com.br/crossbrowser if (obj.addEventListener) obj.addEventListener(evType, fn, true) if (obj.attachEvent) obj.attachEvent("on"+evType, fn) } function removeEvent( obj, type, fn ) { if ( obj.detachEvent ) { obj.detachEvent( 'on'+type, fn ); } else { obj.removeEventListener( type, fn, false ); } } /* THE UPLOAD FUNCTION */ function micoxUpload(form,url_action,id_element,html_show_loading,html_error_http){ /****** * micoxUpload - Submit a form to hidden iframe. Can be used to upload * Use but dont remove my name. Creative Commons. * Versão: 1.0 - 03/03/2007 - Tested no FF2.0 IE6.0 e OP9.1 * Author: Micox - Náiron JCG - elmicoxcodes.blogspot.com - micoxjcg@yahoo.com.br * Parametros: * form - the form to submit or the ID * url_action - url to submit the form. like action parameter of forms. * id_element - element that will receive return of upload. * html_show_loading - Text (or image) that will be show while loading * html_error_http - Text (or image) that will be show if HTTP error. *******/ //testing if 'form' is a html object or a id string form = typeof(form)=="string"?$m(form):form; var erro=""; if(form==null || typeof(form)=="undefined"){ erro += "The form of 1st parameter does not exists.\n";} else if(form.nodeName.toLowerCase()!="form"){ erro += "The form of 1st parameter its not a form.\n";} if($m(id_element)==null){ erro += "The element of 3rd parameter does not exists.\n";} if(erro.length>0) { alert("Error in call micoxUpload:\n" + erro); return; } //creating the iframe var iframe = document.createElement("iframe"); iframe.setAttribute("id","micox-temp"); iframe.setAttribute("name","micox-temp"); iframe.setAttribute("width","0"); iframe.setAttribute("height","0"); iframe.setAttribute("border","0"); iframe.setAttribute("style","width: 0; height: 0; border: none;"); //add to document form.parentNode.appendChild(iframe); window.frames['micox-temp'].name="micox-temp"; //ie sucks //add event var carregou = function() { removeEvent( $m('micox-temp'),"load", carregou); var cross = "javascript: "; cross += "window.parent.$m('" + id_element + "').innerHTML = document.body.innerHTML; void(0); "; $m(id_element).innerHTML = html_error_http; $m('micox-temp').src = cross; //del the iframe setTimeout(function(){ remove($m('micox-temp'))}, 250); } addEvent( $m('micox-temp'),"load", carregou) //properties of form form.setAttribute("target","micox-temp"); form.setAttribute("action",url_action); form.setAttribute("method","post"); form.setAttribute("enctype","multipart/form-data"); form.setAttribute("encoding","multipart/form-data"); //submit form.submit(); //while loading if(html_show_loading.length > 0){ $m(id_element).innerHTML = html_show_loading; } }
2) Include this file in your HTML
<script type="text/javascript" src="micoxUpload.js"></script>
3) Parameters of micoxUpload function:
- form - the form to submit or the ID of a form .
- url_action - url to submit the form. like 'action' parameter of forms.
- id_element - element that will receive return of upload.
- html_show_loading - Text (or image) that will be show while loading
- html_error_http - Text (or image) that will be show if HTTP error.
4) Ok. Now you have a lot of forms to call the Asynchronous upload function. 3 Examples:
4.1) Basic from a button (or input-type-button) in a form:
<legend>Basic use</legend> <form> <input type="file" name="name" /> <div id="upload_1" class="recebe"> </div> <button onClick="micoxUpload(this.form,'upa.php','upload_1','Loading...','Error in upload'); return false;" type="button">test</button> </form> </fieldset>
4.2) When input-file lost focus (onblur):
<fieldset> <legend>On blur use</legend> <form> <input type="file" name="name" onchange="micoxUpload(this.form,'upa.php','upload_2','Loading...','Error in upload')" /> <div id="upload_2" class="recebe"> </div> </form> </fieldset>
4.3) Making acessible whitout JavaScript :
<fieldset> <legend>Unobstrusive</legend> <form action="upa.php" target="_blank"> <input type="file" name="name" onchange="micoxUpload(this.form,'upa.php','upload_3','Loading...','Error in upload')" /> <div id="upload_3" class="recebe"> </div> <button type="submit">Go</button> </form> </fieldset>
Ok. Bugs, errors, comment here.
In next post, a more flexible version of this function for who understend JavaScript.
Portuguese version: Upload Assíncrono (iframe como AJAX) - 1 função simples
I have two remarks:
ReplyDeleteYou should change all "id_elemento_retorno" to "id_element" to make script work.
When you use the "onblur" version in Firefox, the action starts immediately you click on the Browse button. After choosing a file, the action runs again, and uploads the file well. (I havent tested this on other browsers.)
Great job, anyway.
Uh, thanks gyulus.
ReplyDeleteI've forget this.
Its true what you said about "onblur", but just only a example of use.
Very thanks.
:)
Thanks, you have resolved all my problems!!
ReplyDeleteI cant find out the file named upa.php Please give me full source code
ReplyDeletehelo Sivarama.
ReplyDeleteThe upa.php could be any script server-side for receive uploads.
Its like normal upload that you already know.
A example of upload script you can find here.
Another doubts, email-me. :)
Thanks for the script.
ReplyDeleteInstead of "onblur", use "onchange" -- then the upload takes place as soon as the file is selected.
Thanks Anonymous.
ReplyDeleteI will change now.
Oi micox,
ReplyDeletemuito obrigado.
É exactamente o que estava procurando há muito tempo, e parece que está funcionando perfeitamente.
Ainda nao tinha oportunidade para testes com Mac/Safari, mas vou voltar e contar assim que possar.
(O meu Português está horrívelmente enferrugado)
@abhi
ReplyDeleteIntresting :)
@Frits
Ok, meu inglês também não é o melhor do mundo. hehehe
Obrigado.
Hi micox,
ReplyDeletenow I found a severe accessibility problem:
In IE one cannot activate the "Browse" button using the keybord. Tabbing to the upload form and then pressing "Enter" when the focus is on the "Browse" button does not open the file window. It simply does nothing.
In Fx and Op it works as supposed.
I know: IE sucks. But this bug unfortunately renders the script useless for accessible sites :-)
Any idea about this?
Hi Fritz,
ReplyDeleteIn IE, use SPACE BUTTON to open the dialog box and choose your file :-)
Thanks.
Hi micox,
ReplyDeleteSPACE! cool, you made my day. How could one imagine?
Now I've got one more question:
In Mac/Safari your script seems to open a new window (upa.php) showing the results, instead of writing to the <div id="upload...
Any idea about this?
Greetings from sunny Southern Germany
Fritz
Could someone please explain in detail, what Abhi Beckert said on April 7, 2007 6:38 PM
ReplyDeleteAfter replacing all uses of the "standard small functions" with their prototype.js counterparts, this upload script also works in nightly builds of safari.
Wouldn't it be nice, to have this implemented in micox' Script, and have it working in Safari too?
Thanx in advance
Fritz
hello fritz.
ReplyDeleteI will try adapt my script using some lib like jquery to working in Safary.
When i finish, i'll post here.
Thanks.
Great work!
ReplyDeletethis is exactly what i've been looking for and i'm excited to see your project.
I did have a problem getting it up and running though. I'm using java and posting to a jsp file but I cant get the uploads to work. Specifically im using uploadbean to do it.
http://www.javazoom.net/jzservlets/uploadbean/documentation/installation.html
It work's fine standalone, but not when I post to it.
any ideas on what could of gone wrong?
thanks!
I fixed my problem from above!
ReplyDeleteI changed the name of my input file name to "uploadfile" and that was my problem.
thanks!!
Ok Patrick.
ReplyDeleteSorry. I'm late and can't help you.
Good that you fixed your problem.
Thanks,
ReplyDeletehow can use it in form witn submit to ?
Hello Anonymous, please, read the examples in item 4. :-)
ReplyDeleteThanks
@micox, very nice script
ReplyDelete@fritz, is it possible that you post the mentioned prototype counterparts of the 'standard small fuctions'? my first excursus into js and, i know, i'm lazy, too --
* thanks
^
ReplyDelete\
\___ appendix
i forgot:
- works fine in firefox/2.0.0.3; with the following change:
else if (str.toLowerCase(form.nodeName) != "form") { erro += "The form of 1st parameter its not a form.\n"; }
- opens a new window in safari/2.0.4 - which is not really useful for me (see also fritz' comment)
* cheers
Updated anonymous.
ReplyDeleteVery thanks
Very helpful script!
ReplyDeleteDon't use javascript much and was wondering if there was an easy way to load 10 lines of HTML in place of html_show_loading as I'd like to show a pretty formated message while the users wait on the upload.
$m(id_element).innerHTML = html_show_loading;
This can be hard coded in the script and doesn't need to be passed in by the function.
Also wondering if you heard from anyone about addressing some of the issues raised with Safari.
Great help. Thanks for sharing.
// Joff
Thanks Joff
ReplyDeleteI'll work in a new version of this function, based in all help that i've receive in the comments.
The problem with safari is that i dont have any browser safari for the test, but i'll search a way to resolve this.
Thanks.
That's great news! Would love to help test your next version.
ReplyDeleteIt would be very cool to have a PC emulator of Safari. Did a quick search and did not find.
Do you have any thoughts on how to hard code a few lines of HTML text into innerHTML in place of the html_show_loading var?
Appreciated,
joff
@joff:
ReplyDeleteas easy as this:
$m(id_element).innerHTML = "<p><img src=\"waiting.gif\" alt=\"\" border=\"0\" width=\"95\" height=\"11\" />uploading ...</p>";
have a look at the result at:
http://webdesign.weisshart.de/chat/chat.php
Safari Emulator? better than that: A real Safari remotely controlled at:
http://www.browserpool.de/kc/wob/portal.jsp?lang=en
The free test account does a perfect job
greetings
Fritz
F-
ReplyDeleteThanks for the pointer. Wasn't escaping things properly :(
WRT to a remote controlled Safari...that's cool!
Take care,
Joff
Got micoxUpload working in my webapp. Butt I'm still curious about Abhi Beckert's comment on April 7, 2007 about how he got micoxUpload working in Safari...
ReplyDeleteSince Safari has become available for Windows (http://www.apple.com/safari/download/, still in beta) we PC guys have no excuses left for not making our webapps compatible with this Mac browser.
So Abhi, could you please point us in the right direction?
Hey micox, parabéns por ter seu script publicado no Smashing Magazine.
ReplyDeleteSucesso e continue com o excelente trabalho.
http://www.smashingmagazine.com/2007/07/09/best-of-mayjune-2007/
Abraços,
canha.
Nice Scripting.
ReplyDelete-Jamie
Hey there,
ReplyDeletetks for this, i really like it.but i can't use it inside an other form whith submit fuction
Thank you for you help
Nice script. I was wondering if someone could help me out though. I am going to be using it for uploading images and what I want to do is show the image after it is uploaded but I dont know how to return the image. Can anyone help me out?
ReplyDeleteThanks
doom
Hi anonymous.
ReplyDeleteTo do that you just need send back the tag img with the new url of the uploaded image.
More doubts, send a email :)
I've had great success with the script on my site but it doesn't work with the new Safari. Did any of the folks familiar with Safari offer suggestions on how to fix?
ReplyDeleteAppreciated,
Joff
I have tested this script on Firefox 2.0 and i can't see the image I have uploaded becaue Firefox has a problem with the iframes. How did you do to enable this on FF? thanks in advance,
ReplyDeleteAlessio
Hi Ace of Spades (motorhead rules heheh), i've tested my script now and its ok. No problems with FF2.
ReplyDeleteIf you want, i can send to you the php script that store the file in the server to use with my js-script for upload.
awesome code man! Just what I needed. I am using it to add image upload to sugarcrm.
ReplyDeleteWhen I upload image, I dont want it to show the browse file input field again. How do I stop it from re-creating that field?
Hi Richard.
ReplyDeleteYou must delete the input element when you call my function.
Try Put:
form.parentNode.removeChild(form)
above the line:
$m(id_element).innerHTML = html_error_http;
This is not working my friend.
ReplyDeleteI tried different variation, like adding it to a setTimeout call and but doest work.
Can you post a different way to remove the input field?
I noticed that on http://webdesign.weisshart.de/chat/chat.php
ReplyDeletewhen you upload a file, it does not repeat the input field form, and then I looked at the .js file http://webdesign.weisshart.de/chat/micoxUpload.js
And its exactly the same... no need to remove anything, it just does not repeat the input file.
I am going crazy on this, how to remove the repeated input. Please some on help on this!
Hi richard, try this:
ReplyDeleteafter:
setTimeout(function(){ remove($m('micox-temp'))}, 250);
put:
form.reset();
Hi, thanks for your reply...
ReplyDeleteThat does not work either...
You can view my working version here:
http://printerzone.net/uploadimages/testajax.php
http://printerzone.net/uploadimages/testajax.js
What I need done is that when you select the image file to upload...
#1 the initial input field disappears (done)
#2 a thumbnail of the image is shown (done)
#3 Do not show again the input field (MISSING)
Thanks for your help!
Ops. sorry.
ReplyDeletePut this above that setTimeout:
var inp = form.getElementsByTagName('input')
for(var s in inp){ if(inp[s].type=='file'){
form.removeChild(inp[s]);
}}
This will remove the input-file.
Nope, thats not working either. You can test out my code in the link above and you will see yourself.
ReplyDeleteYou can also look at the js file and you will see that i placed the properly.
FireFox FireBug is giving me this error...
Node was not found" code: "8
[Break on this error] form.removeChild(inp[s]);
Hei, your server side script testajax.php that is returning the input-file aggain.
ReplyDeleteYou need to put a IF in your server-side script (testajax.php) to not print the form when you upload a image. :)
The solution its simple heheheh.
When you upload a image, just print this image and not print the form again.
Hi, yeah that was the problem.
ReplyDeleteAnother issue now...
I need to reference the form in another way, not this.form, because I have already others forms referenced like that.
So I tried the following...
#1 adding an id to the form and adding micoxUpload('formId','upload.php'... (DID NOT WORK)
#2 adding a name to the form and adding micoxUpload('formName','upload.php'... (DID NOT WORK)
I also tried not using the qoutes '' but then it doesn't work at all.
I just to figure out a way to identify the form that is NOT this.form
Thanks Again!
actually I found out the issue is that this form were are using is inside another form. So that's why I cannot refer to it as this.form and also why referring to its id doesnt work either.
ReplyDeleteI need to come up with some savvy js code the reference the id of a form inside another form
You think you can give me a hand on this Micox? I' trying to create a field in sugarm crm that allows image upload.
Thanks! I welcome any ideas
Just in case someone uses SugarCRM and is interested in creating a field type that allows image upload, this is the thread I started and of coarse I am using the code from Micox.
ReplyDeletehttp://www.sugarcrm.com/forums/showthread.php?t=26723
Hi Micox,
ReplyDeleteIt would be really nice of you if you could drop by and give some feedback in trying to implement your code into sugarCrm.
I have detailed the progress and issues, so please I inivite you to take a look and see if you can contribute with your expertise.
Thanks!
http://www.sugarcrm.com/forums/showthread.php?t=26723
Hi Richard. I will see your forum, but in next comunications send-me a email (or tell your email).
ReplyDeletebye, thanks :)
Hi Micox,
ReplyDeletevery nice script, saved me a lot of time!
I'm not much of a JS programmer so I have a question:
I made the server-side script in php, and if the file uploads correctly it returns 1 and if not 0. If the file uploaded ok I need to call another function, and if not just write something in id_element. Any ideas?
Yeah, anonymous. I made a new function that accepts a callback function and you will do this that you want.
ReplyDeleteSend me a email and i will send the new code to you.
hi micox..
ReplyDeleteperfect script very well done.. this is what exactly what im looking for..
there were some bugs but they are all have been fixed myself.
i'll be implementing this script to one of my big web project and i'll surely put your name there as part of script contributor..
VERY WELL DONE..
This comment has been removed by the author.
ReplyDeleteLovin' the script and got it working perfectly on the first try, I just have a simple request:
ReplyDeleteIs there a way to have the script not erase the result of the upload?
Basically I want it to append the new upload info echoed from upa.php on top of the old ones to generate a list of all uploaded files.
Any help would be much appreciated!
Hi h00rj
ReplyDeleteI will post in this week a new version of upload script where i will explain a example to do this that you want. ok?
I'll send a email to you.
Thanks.
Amazing Job, just saved me a weeks worth of work!
ReplyDeleteMucho Kudos to you my friend!
Thanks for this, use lots of AJAX on my sites and needed to incorporate file uploading with progress bar for client. This does the trick. Well done. So simple, but then the best solutions are...
ReplyDeleteWell this a great idea, i have used your script, the script is working good, but the addEvent & remove Event thing is not working properly for me in mac safari , i am trying to fix this, the problem is that mac safari is not detecting the "iframe load event Listener" , if u can help me out in this?
ReplyDeleteBut last & not the list great idea.
Mail me on "sinash2003@gmail.com"
If the id_element - element that will receive return of upload - has the same parent Node as the Form, you will run into difficulties.
ReplyDeleteWhy not try something simple like this?
function Uploader(form, urlAction, id_returnElement, html_loadMessage, html_HttpError){
this.oForm = typeof(form) == "string" ? this.mId(form) : form;
this.iFrameObjId = null;
this.httpError = html_HttpError;
this.urlAction = urlAction;
this.htmlOnLoading = html_loadMessage;
this.returnElementId = id_returnElement;
this.timeoutId = null;
this.createIFrame();
}
Uploader.prototype.mId = function(sId){
return document.getElementById(sId);
}
Uploader.prototype.createIFrame = function(){
var iframe = document.createElement("iframe");
iframe.setAttribute("id","hiddenFrame");
iframe.setAttribute("name","hiddenFrame");
iframe.setAttribute("width", "0");
iframe.setAttribute("height","0");
iframe.setAttribute("border","0");
iframe.setAttribute("style", "width: 0; height: 0; border: none;");
var oNode = this.mId("buffer");
oNode.appendChild(iframe);
window.frames['hiddenFrame'].name="hiddenFrame";
this.iFrameObjId = this.mId("hiddenFrame");
}
Uploader.prototype.removeNode = function(oId){
var oNode = oId;
if(typeof oNode == 'object' && oNode != null){
oNode.parentNode.removeChild(oNode);
this.iFrameObjId = null;
}
}
Uploader.prototype.addEvent = function(oId, sEventtype, fn){
if (oId.addEventListener){
oId.addEventListener(sEventtype, fn, true);
}
// IE
else if(oId.attachEvent){
oId.attachEvent("on"+sEventtype, fn);
}
}
Uploader.prototype.removeEvent = function( obj, type, fn ) {
if(obj.detachEvent) {
obj.detachEvent('on'+type, fn);
} else {
obj.removeEventListener( type, fn, false );
}
}
Uploader.prototype.doUpload = function(){
var oThis = this;
var OnUploadCompleted = function (){
oThis.removeEvent(oThis.mId("hiddenFrame"), "load", OnUploadCompleted);
window.parent.clearTimeout(oThis.timeoutId);
window.parent.document.getElementById(oThis.returnElementId).innerHTML = window.frames['hiddenFrame'].document.body.innerHTML;
};
this.addEvent(this.mId("hiddenFrame"), "load", OnUploadCompleted);
this.oForm.setAttribute("target", "hiddenFrame");
this.oForm.setAttribute("action", this.urlAction);
this.oForm.setAttribute("method", "POST");
this.oForm.setAttribute("enctype", "multipart/form-data");
this.oForm.submit();
this.mId(\"loadMessage\").innerHTML = this.htmlOnLoading;
this.timeoutId = window.setTimeout(function(){oThis.mId('loadMessage').innerHTML = oThis.httpError;}, 20000);
}
Joachim
Sorry that was nonsense. You will run into difficulties, if the Id of the element, which will receive the return of the Upload is the parent Node of the form. You append the IFrame to the parent Node of the form. So the returned html will overwrite the IFrame.
ReplyDeleteJoachim
Thanks joachim,
ReplyDeleteI had not think about this problem.
Thanks.
Here is a new version of my function Uploader. Basically I added the line
ReplyDeletethis.oForm.setAttribute("encoding", "multipart/form-data");
which is necessary for IE to submit the form properly. At the end of the function OnUploadCompleted which is called when the Iframe reloads I added the timeout to delete the Iframe. It is necessary to delete the Iframe for IE to redo the Upload, otherwise the function Uploader will not work, if called a second time. On the other hand if you delete the Iframe, Mozilla won't work properly if the function Uploader is called a second time.
Now it works in IE and Mozilla Browsers.
Joachim
// Uploader
function Uploader(form, urlAction, id_returnElement, html_loadMessage, html_HttpError){
this.oForm = typeof(form) == "string" ? this.mId(form) : form;
this.iFrameObjId;
this.httpError = html_HttpError;
this.urlAction = urlAction;
this.htmlOnLoading = html_loadMessage;
this.returnElementId = id_returnElement;
this.timeoutId = null;
this.createIFrame();
}
Uploader.prototype.mId = function(sId){
return document.getElementById(sId);
}
Uploader.prototype.createIFrame = function(){
var iframe = document.createElement("iframe");
iframe.setAttribute("id", "hiddenFrame");
iframe.setAttribute("name", "hiddenFrame");
iframe.setAttribute("width", "0");
iframe.setAttribute("height", "0");
iframe.setAttribute("border", "0");
iframe.setAttribute("style", "width: 0; height: 0; border: none;");
var oNode = this.mId("buffer");
oNode.appendChild(iframe);
window.frames['hiddenFrame'].name="hiddenFrame";
this.iFrameObjId = this.mId("hiddenFrame");
}
Uploader.prototype.removeNode = function(oId){
var oNode = oId;
if(typeof oNode == 'object' && oNode != null){
oNode.parentNode.removeChild(oNode);
}
}
Uploader.prototype.addEvent = function(oId, sEventtype, fn){
if (oId.addEventListener){
oId.addEventListener(sEventtype, fn, true);
}
// IE
else if(oId.attachEvent){
oId.attachEvent("on"+sEventtype, fn);
}
}
Uploader.prototype.removeEvent = function( obj, type, fn ) {
if(obj.detachEvent) {
obj.detachEvent('on'+type, fn);
} else {
obj.removeEventListener( type, fn, false );
}
}
Uploader.prototype.doUpload = function(){
var oThis = this;
var OnUploadCompleted = function (){
oThis.removeEvent(oThis.mId("hiddenFrame"), "load", OnUploadCompleted);
window.parent.clearTimeout(oThis.timeoutId);
alert(window.frames['hiddenFrame'].document.body.innerHTML);
window.parent.document.formEditor.picture_id.value = window.frames['hiddenFrame'].document.uploadForm.picture_id.value;
window.parent.document.getElementById(oThis.returnElementId).innerHTML = window.frames['hiddenFrame'].document.body.innerHTML;
// del the iframe if internet explorer
if(oThis.mId('hiddenFrame').attachEvent){
window.parent.setTimeout(function(){oThis.removeNode(oThis.mId('hiddenFrame'))}, 250);
}
};
this.addEvent(this.mId("hiddenFrame"), "load", OnUploadCompleted);
this.oForm.setAttribute("target", "hiddenFrame");
this.oForm.setAttribute("action", this.urlAction);
this.oForm.setAttribute("method", "POST");
this.oForm.setAttribute("enctype", "multipart/form-data");
// IE
this.oForm.setAttribute("encoding", "multipart/form-data");
this.oForm.submit();
this.mId("loadMessage").innerHTML = this.htmlOnLoading;
this.timeoutId = window.setTimeout(function(){oThis.mId('loadMessage').innerHTML = oThis.httpError;}, 20000);
}
This comment has been removed by the author.
ReplyDeleteHi there, thanks for the great script . Ive tried a few and this is the easiest to you by far
ReplyDeleteThx
Question is there a way i can limit the number downloads eg 12. Ive tried replicating the input field but it gives me strange results if i have more than one in a form.
okay.... i got a question..
ReplyDeleteso basically this js didn't upload anything, right?
I have to make my own the 'upa.php' with php function like
@move_uploaded_file
@rename
..etc
isn't it?
How about adding an ajax upload progress bar? any idea how to do that?
Yes. You have to make your own upa.php.
ReplyDeleteYou can see progressbars just with flash (swf).
Hi Micox,
ReplyDeleteI appreciate your great work and am very thankful about you publishing your great work.
Keep on the good work!
I really appreciate the work. But i am facing one critical issue and would be happy if any of you will be able to provide the solution for me.
ReplyDeleteI am uploading a xls file using the iframe upload method and then i am doing some database operation in the server side which is taking about 1-2 mins to complete. The wierd thing is , though the response was sent by the server program(servlet), the javascript was not getting the response and posting the same request again to the server. It shows the response only in the second time.
Can anyone out there able to help out on this issue.
Thanks
urs_amt
I have a new version if you want to test before use.
ReplyDeletethe script: http://naironjcg.googlepages.com/micoxUpload2.js
The examples (see de source) : http://naironjcg.googlepages.com/upload5.htm (in this examples, the upload dont work because googlepages.com dont support)
Thanks.
I tried to use your script (micoxUpload2.js) with IE and FireFox - everything was ok, but when I try to upload a file using Opera (9.27) something goes wrong.
ReplyDeleteAbout the problem with Opera, which I mentioned before:
ReplyDeleteIt looks strange, but if I insert a string like that:
'alert("sometext");'
just before the following line in the script:
'timeload = setInterval(periodic,500);'
the script starts working with Opera (9.27) without any problems
Any ideas?
Micox, great script!
ReplyDeleteCould you share with us how would I be able to have multiple uploads in a single form?
Ok, I found a way to allow multiple uploads in a single form..
ReplyDeleteThis may not be the only/best way but it works for me =)
Add the following code after
form.submit();
//reset form back to original state
form.removeAttribute("target"); form.removeAttribute("action"); form.removeAttribute("method"); form.removeAttribute("enctype"); form.removeAttribute("encoding");
form.setAttribute("action","saveform.php"); form.setAttribute("method","post");
ofcourse you should change the attributes to whatever is needed for your case. that's just what I used for mine.
Thankx for this code!
ReplyDeleteIt really helped a lot.
hi ,
ReplyDeleteThanks for uploading the script.
I am trying to integrate ur script on my page. I used the php from http://www.w3schools.com/PHP/php_file_upload.asp . But when i try to upload a file .. it always gives me 'Invalid File' error . Is there a solution to this ?
Hi,
ReplyDeleteYour image must respect this conditions:
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
But its not a restriction of my upload script but of the php script.
Thanks.
I am developing a major new product and am thrilled with your microX. However, I have found that it does not work with either Safari 3.1.2 for Windows, or the Google Chrome 0.3.154.9 (these two seem to have some shared code).
ReplyDeleteIt works without problem on Firefox 3.0.3, IE 6.x, and Opera 9.51 (I don't bother much with Netscape anymore).
Can you provide an updated version that will also work with the current versions of Safari and Chrome?
Thank you!
drdave
... and, on the MAC, Safari 2.0.4 gets the correct result, but puts it into a separate window...
ReplyDeletedrdave
Man, I have been racking my brain trying too get this to work with chrome....
ReplyDeleteI love your work I have modified it slightly to meet my needs but I just cannot get it.
I have nrowed it down to the function remove()
it only contains this:
theVar.parentNode.removeChild(theVar);
Any ideas???
Well, it took 2 days but I finally got this to work in all modern browsers (IE6, IE7, Firefox 2+, Opera, Safari, Chrome), etc...
ReplyDeleteIts pretty simple actually apples webKit is where the issue lies, so I just decided to detect it by adding this function:
function isWebKit(){
return RegExp("AppleWebKit/").test(navigator.userAgent);
}
Then right after the first line of the function micoxUpload() add the following:
var detectWebKit = isWebKit();
Then the last step is replace:
setTimeout(function(){ remove($m('micox-temp'))}, 250);
With:
if(detectWebKit){
remove($m('ajax-temp'));
}else{
setTimeout(function(){ remove($m('micox-temp'))}, 250);
}
Hope this helps someone, I cannot tell you how many hours I wasted on this!
comments? tim {at} atwebresults {dot} com
or see it in an app:
http://restaurantmenuscript.com/
wow man thats awesome!!!
ReplyDeleteHi Micox
ReplyDeleteThanks for the code.
I just feel the Script is working. But I didn’t get the exact output. Please advice me...
I just copy all your recommended scripts and code.
But see when I use upa.php as that sentenced in the below URL
http://www.w3schools.com/PHP/php_file_upload.asp
But the output I found is
Upload:
Type:
Size: 0 Kb
Temp file:
already exists.
Feel that I am not able to store the uploaded image in the recommended folder called "Upload"....Kindly advice me.....If any one of this Commented zone.....
Get me @ jothirajan.veerabathiran@makeeit.com
Or
jothirajan.veerabathiran@makeeitsolutions.com
Thanks a lot......
ReplyDeleteThe first code from the web that works in front me...
Excellent script..Just cut-copy-paste...
Great......i first thought that "without submitting a form" how can the script upload the image in the folder,but your script makes the JS upload in a fantastic way.
Thanks once again...
Best and Smart code...
And once again thanks for your quick reply to my personal ID....Thanks once again
Thanks a lot......
ReplyDeleteThe first code from the web that works in front me...
Excellent script..Just cut-copy-paste...
Great......i first thought that "without submitting a form" how can the script upload the image in the folder,but your script makes the JS upload in a fantastic way.
Thanks once again...
Best and Smart code...
And once again thanks for your quick reply to my personal ID....Thanks Náiron JCG
Yes man i got it, but the thing is i cant understand the Portuguese language...You know i am using ur code for the video conversion just like that of the Youtube.Thanks man...
ReplyDeleteOne thing if you have any code to show the loading image or text for the page delay.For example if i am in the index.html and jump to reg.html it takes some certain time to go to
that page, at that time the site become zigzag and white blank...
Thing is can you advice me...To show something in that particular period of time( page delay)...Or anyone from this forum zone....
Thanks and Regards
jothirajan.V
THANKS MICOX FOR YOUR AWESOMENESS!!!
ReplyDeleteAND THANKS TIM FOR THE SAFARI FIX!!!
This comment has been removed by the author.
ReplyDeleteI get only:
ReplyDeleteParse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/antreee/public_html/test/up.php on line 33
up.php taken from http://www.w3schools.com/PHP/php_file_upload.asp. Any ideas? :s
This comment has been removed by the author.
ReplyDeleteNeed help in resolving the issue of micoxUpload.js Safari support
ReplyDeleteThanks Tim for the Safari fix. I really found it very helpful.
There is an issue which I am still facing in Safari.
The issue which I am facing is that the "carregou" function is not getting executed properly. Even though successful response from server has been received, the "innerHTML" of "micox-temp" is always "html_error_http" in Safari. For Firefox 3.0.7 and IE 6.0, the code is working fine.
Following is the code which I am trying to execute:
function isWebKit(){
return RegExp("AppleWebKit/").test(navigator.userAgent);
}
function micoxUpload(form,url_action,id_element,html_show_loading,html_error_http,micoFileId, callback){
/******
* micoxUpload - Submit a form to hidden iframe. Can be used to upload
* Versão: 1.0 - 03/03/2007 - Tested no FF2.0 IE6.0 e OP9.1
* Parametros:
* form - the form to submit or the ID
* url_action - url to submit the form. like action parameter of forms.
* id_element - element that will receive return of upload.
* html_show_loading - Text (or image) that will be show while loading
* html_error_http - Text (or image) that will be show if HTTP error.
*******/
//testing if 'form' is a html object or a id string
form = typeof(form)=="string"?$m(form):form;
var detectWebKit = isWebKit();
var erro="";
if(form===null || typeof(form)==="undefined"){ erro += "The form of 1st parameter does not exists.\n";}
else if(form.nodeName.toLowerCase()!="form"){ erro += "The form of 1st parameter its not a form.\n";}
if($m(id_element)===null){ erro += "The element of 3rd parameter does not exists.\n";}
if(erro.length>0) {
return;
}
//creating the iframe
var iframe = document.createElement("iframe");
iframe.setAttribute("id","micox-temp");
iframe.setAttribute("name","micox-temp");
iframe.setAttribute("width","0");
iframe.setAttribute("height","0");
iframe.setAttribute("border","0");
iframe.setAttribute("style","width: 0; height: 0; border: none;");
//add to document
form.parentNode.appendChild(iframe);
var fileName=$m(micoFileId).value;
window.frames['micox-temp'].name="micox-temp"; //ie sucks
//add event
var carregou = function() {
removeEvent( $m('micox-temp'),"load", carregou);
$m(id_element).innerHTML = html_error_http;
//del the iframe
//setTimeout(function(){ remove($m('micox-temp'))}, 250000);
if(detectWebKit){
remove($m('micox-temp'));
}else{
setTimeout(function(){ remove($m('micox-temp'))}, 250000);
}
};
addEvent( $m('micox-temp'),"load", carregou);
//properties of form
form.setAttribute("target","micox-temp");
form.setAttribute("action",url_action+"&fileName="+fileName);
form.setAttribute("method","post");
form.setAttribute("enctype","multipart/form-data");
form.setAttribute("encoding","multipart/form-data");
iframe.callback=callback;
//submit
form.submit();
//while loading
if(html_show_loading.length > 0){
$m(id_element).innerHTML = html_show_loading;
}
}
Can Tim/anyone please suggest some solution for it. Thanks in advance for looking into my problem.
My email id is: amitrnihar@gmail.com
Thanks,
Nihar.
I keep getting an error
ReplyDeleteError in upload
and I have included the javascript file micoxUpload and used form #1 from your examples. Please help get this fixed.
I have an ASP.NET page with micoxUploader. When i test it on localhost server everything is all right, but when I test it on other server, I have a bug.
ReplyDelete1)press on browse button fist time
2)choose img file
3)file uploads on server
4)click any place on page
5)file uploads once more
Please, any body help me.
hello.
ReplyDeletewhat is in the upa.php ?
can you show me a examble .
mfg
There's a problem when you have multiple upload buttons in the same page/form.
ReplyDeleteWhen you start an upload, and start another one while the first one is still uploading. When both uploads are finished, they both return the last file uploaded - even though the actual files have been uploaded.
Any remedy to this?
Thanks!
finally, I got it to work for me in
ReplyDeleteFirefox 3.5
Opera <= 10.0
Safari/Win
Chrome
IE 6.0 ... 8
Opera <= 10.0
(had to use micoxUpload2.js
Hi,
ReplyDeleteFirst of all, thanks a lot for your code. This is exactly what I was looking for. If have integrated the same into my page. Have one query though, I want to show my users that file upload was successful when the upload process has completed sucessfully.
Is there anyway, that I can do it?
Thanks once again
Jameel
work great for me. thanks
ReplyDeleteOlá Micox,
ReplyDeleteEstou a ter um problema na função "carregou" na variável "cross", o erro é o seguinte: "Reference error: Window is not defined".
Já viste alguma vez este problema?
Bom código :)
Obrigado,
JP.
@Prata
ReplyDeleteUmmm problema novo desconhecido.
Deve ter a ver com o navegador.
Tente remover o window. que tem aí nesta função pra ver o que acontece.
Até mais.
Fiz uma mudança no código e ele agora também funciona no CHROME e SAFARI.
ReplyDeleteSubstitui:
var carregou = function() {
removeEvent( $m('micox-temp'),"load", carregou);
var cross = "javascript: ";
cross += "window.parent.$m('" + id_elemento_retorno + "').innerHTML = document.body.innerHTML; void(0); ";
$m(id_elemento_retorno).innerHTML = html_erro_http;
$m('micox-temp').src = cross;
//deleta o iframe
setTimeout(function(){ remove($m('micox-temp'))}, 250);
}
por:
var carregou = function() {
window.document.getElementById( id_elemento_retorno ).innerHTML = 'Arquivo enviado com sucesso';
}
Dessa forma ele a mensagem de retorno vai normalmente.
Tim, your tip made my day! I was in touch w/ Micox trying to solve Chrome´s incompatibility problem e your tip solved it all. Thank you very much.
ReplyDeleteSomeone could compile and test all suggested and approved mods and launch a micox v2.1, cross-browser version. Anybody?
ReplyDeleteI think i ve learn something today, I tried it with safari browser and it works great, Thanks a lot for the code
ReplyDeletefunction remove(quem) {
ReplyDeletequem.parentNode.removeChild(quem);
}
This is giving error on IE9 :- parentNode is null. Don't know where others are testing as its a major dent using iframes. Any workaround?
Hi,
ReplyDeleteit is nice script , i used to upload cv.if i upload doc file more than 2 mb it is showing 'Loading' text. please help ...
Thanks