Jump to content

Please Give Clarification Of Upload Ways


abrakatabra
 Share

Recommended Posts

Hello all!Can you please help to find answers on my questions?it is known that the Internet has some ways to submit a form without page reloading.One of them is iframe creation inside a page and assign onload callback to react for response of a server script.Here is an example of iframe creation which I can find from the Internet:

function createIframe(...) {...var n = 'f' + Math.floor(Math.random() * 99999);var d = document.createElement('DIV');d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="onResponse(this)"></iframe>';

1. Above code is called on each submit. So why there is a need to create one iframe per one submit?2. in case I need one iframe can I declare it using original html tag and do not use javascript? Why javascript is better in case is better?like this

<iframe id="response" style="display:none" name="response" src="about:blank" onload="onResponse(this)"></iframe>

May be latest question seems strange but I wounder why a lot of examples create iframes by javascript. I guess write html code simpler than javascript one.

Link to comment
Share on other sites

Thanks, Kingy and Synook!As far as understand ajax can't upload files by security reason so the trick with iframe is used.

<form action="#" onsubmit="myFunction();return false">
it supposes myFunction() gets values from input objects and sends them through xmlhttp.send()where xmlhttp was created as
if (window.XMLHttpRequest)		{			getXmlHttpObject.xmlhttp = new XMLHttpRequest();		}		if (window.ActiveXObject)		{			getXmlHttpObject.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");		}

I can get filename (but it is not so easy as full path of file depends on OS and browser) but I cannot upload file with ajax as I said above.Am I wrong?

Edited by Alexander
Link to comment
Share on other sites

You can create a file upload with AJAX but you'll probably need a touch of server side stuff like PHP.Besides iframes are like a decade old and practically unused. : ]I just quickly googled a quick example, you can find loads of similar stuff from there.Just search for AJAX file upload or something.http://www.ajaxf1.com/tutorial/ajax-file-u...d-tutorial.html

Link to comment
Share on other sites

You can create a file upload with AJAX but you'll probably need a touch of server side stuff like PHP.Besides iframes are like a decade old and practically unused. : ]I just quickly googled a quick example, you can find loads of similar stuff from there.Just search for AJAX file upload or something.http://www.ajaxf1.com/tutorial/ajax-file-u...d-tutorial.html
Thanks. But link which you post describes the same way. But the author does not create iframe dynamically instead he/she writes html tag called iframe.Probably dynamic or static creation of iframe depends on what I what to do. Edited by Alexander
Link to comment
Share on other sites

AJAX can send form data, but can't upload files, so the iframe is necessary. All ExtJS allows you to do is to edit the file path in the form field.If I have a file uploading form, I would have the <iframe> element already there, rather than creating a new one upon each form submission.To answer both your questions:

  1. You don't need a new frame for each form submission
  2. It probably is better to have the iframe written in HTML than create it in Javascript

When the onRelease() function is called, check the content of the <iframe> to see if the server sent an error message of a success one.

Link to comment
Share on other sites

AJAX can send form data, but can't upload files, so the iframe is necessary. All ExtJS allows you to do is to edit the file path in the form field.If I have a file uploading form, I would have the <iframe> element already there, rather than creating a new one upon each form submission.To answer both your questions:
  1. You don't need a new frame for each form submission
  2. It probably is better to have the iframe written in HTML than create it in Javascript

When the onRelease() function is called, check the content of the <iframe> to see if the server sent an error message of a success one.

Thank you for help!
Link to comment
Share on other sites

Yeah, much to my dismay, the iFrame technique is the ONLY one that works right now. You can get results when you Google file upload ajax, but they ALL turn out to use the hidden iFrame trick, which is "ajax" only by an extreme stretch of the definition. I hate the technique. It's an ugly kludge that violates a lot of principles I stick to when I program. But it's all we have for now. I'm sure the javascript designers could develop a secure way handle it using real ajax, and I don't understand why they haven't done so.I also agree with Ingolme's two points. I have a project where you can submit many types of files to a variety of PHP scripts, and I only use one iFrame and one file input. Depending on the context, the script changes some of the hidden parameters in the form so that the capture and callback functions know what to expect.

Edited by Deirdre's Dad
Link to comment
Share on other sites

All ExtJS allows you to do is to edit the file path in the form field.
ExtJS will in fact treat it as a regular ajax request, as far as I know when I'm writing ExtJS I handle a file upload like any other ajax request. But if I'm looking at the requests going out, I can tell that it does not submit using normal ajax techniques, instead it creates the iframe and submits to that, and then automatically reads the response in the iframe and makes that available to the ajax code like a normal ajax response. So ExtJS just abstracts the iframe functionality to make it appear to the programmer as if it was a normal ajax request and response.
I'm sure the javascript designers could develop a secure way handle it using real ajax, and I don't understand why they haven't done so.
The problem there is allowing Javascript to read any file on the client and send it to the server. That's a no-no. I believe Mozilla is working on a method to do just that, so we'll see how that turns out.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...