Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>jQuery Ajax code</p> <pre><code>jQuery('document').ready(function(){ var input = document.getElementById("imagefile"); var formdata = false; if (window.FormData) { formdata = new FormData(); } input.addEventListener("change", function (evt) { var i = 0, len = this.files.length, img, reader, file; for ( ; i &lt; len; i++ ) { file = this.files[i]; //validation to check whether uploaded files are images if (!!file.type.match(/image.*/)) { if ( window.FileReader ) { reader = new FileReader(); reader.onloadend = function (e) { }; reader.readAsDataURL(file); } if (formdata) { //send the ajax query to upload the file(s) jQuery.ajax({ url: "upload.php", type: "POST", data: formdata, processData: false, contentType: false, success: function (result) { jQuery('div#response').html("Successfully uploaded").fadeOut(); } }); } } else { alert('Not a vaild image!'); } } }, false); }); </code></pre> <p>HTML Code</p> <pre><code>&lt;form enctype="multipart/form-data"&gt; &lt;input id="imagefile" type="file" name="image" accept="image/*"/ /&gt; &lt;/form&gt; </code></pre> <p>What above code will do for you is, once you upload the file(s) it will 1st of validate the type of the image and upload it via ajax for you. You can do <strong>multiple file uploads</strong> if you need. If you ever wonder what <code>FormData</code> does,</p> <blockquote> <p>The <code>FormData</code> object lets you compile a set of key/value pairs to send using XMLHttpRequest. Its primarily intended for use in sending form data, but can be used independently from forms in order to transmit keyed data. The transmitted data is in the same format that the form's submit() method would use to send the data if the form's encoding type were set to "multipart/form-data".</p> </blockquote> <p>What addEventListener does for you is,</p> <blockquote> <p>addEventListener() registers a single event listener on a single target. The event target may be a single element in a document, the document itself, a window, or an XMLHttpRequest.</p> </blockquote> <p>You can use <code>upload.php</code> to upload the file(s) at the server level. </p> <p>If you want to have the this functionality in a plugin you can extend the plugin with the above code and it will not brick your plugin because this is a very straight forward code snippet which gets the job done. I have successfully integrated the code to a plugin with no issues at all.</p> <p>this link will show you how to validate for the file size. <a href="https://stackoverflow.com/questions/3717793/javascript-file-upload-size-validation">JavaScript file upload size validation</a></p> <p>Re-size it to fit within some particular dimensions <a href="https://stackoverflow.com/questions/170624/javascript-image-resize">javascript Image Resize</a></p> <p><a href="https://stackoverflow.com/questions/3971841/how-to-resize-images-proportionally-keeping-the-aspect-ratio">How to resize images proportionally / keeping the aspect ratio?</a></p> <p>These link should help for you. I'm not going to integrate all into one because to maintain the simplicity of the code so you can understand easily. </p> <p>If you have any issues let me know.</p>
    singulars
    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.
    2. 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