Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming you have a handle on the server side.. here is a small function and example on how to implement the 'iframe hack' in javascript. </p> <p><strong>html</strong> </p> <pre><code>&lt;form name="image-upload"&gt; &lt;input type="file" name="image" /&gt;&lt;/br&gt; &lt;button type="submit" name="upload"&gt;Upload&lt;/button&gt; &lt;div id="upload-results"&gt;&lt;/div&gt; &lt;/form&gt; </code></pre> <p><strong>javascript</strong> </p> <pre><code>var fileUpload = function(form /* HTMLElement */, action /* Form Action URL */, callback /* Callback function */) { /* vars */ var atribs = { "target": "upload_iframe", "action": action, "method": "post", "enctype": "multipart/form-data", "encoding": "multipart/form-data" }, iframe; /* iframe listener */ var ilistener = function() { var results; listener.remove(this, 'load', ilistener); if( 'contentDocument' in this ) { results = this.contentDocument.body.innerHTML; } else if ( 'contentWindow' in this ) { results = this.contentWindow.document.body.innerHTML; } else if ( 'document' in this ) { results = this.document.body.innerHTML; } else { throw "i'm dead jim :/"; } callback.apply(this,[results]); // call the callback, passing the results this.parentNode.removeChild(this); // remove the iframe }; /* create the iframe */ form.parentNode.appendChild(FragBuilder([{"tagName": "iframe","id": "upload_iframe","name": "upload_iframe","style": {"height": "0","width": "0","border": "0"}}])); /* collect the iframe back */ iframe = By.id('upload_iframe'); /* set the form properties */ for( var attr in atribs ) { if( attr in form ) { form[attr] = atribs[attr]; } } /* attach the event listener to the iframe */ listener.add(iframe, 'load', ilistener); /* submitting the form */ form.submit(); }; // get the form, and the results area var form = document.forms['image-upload'], results = By.id('upload-results'); // listen for the form submit, capture it, and run the iframe upload. listener.add(form, 'submit', function(e) { e.preventDefault(); results.innerHTML = 'Uploading...'; fileUpload(this, 'server.php' /* really anything */, function(data) { console.log(data); results.innerHTML = "Uploaded!"; }); }); </code></pre> <p>Please note: for simplicity purposes I have used the following utilities. <a href="https://github.com/rlemon/FragBuilder.js" rel="nofollow">https://github.com/rlemon/FragBuilder.js</a> DocumentFragment builder from JSON input.<br> <a href="https://gist.github.com/2172100" rel="nofollow">https://gist.github.com/2172100</a> event listener, and By utility functions.<br> *these are both easily removed. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload