Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Iframes is no longer needed for uploading files through ajax. I've recently done it by myself. Check out these pages:</p> <p><a href="https://stackoverflow.com/questions/4006520/using-html5-file-uploads-with-ajax-and-jquery">Using HTML5 file uploads with AJAX and jQuery</a></p> <p><a href="http://dev.w3.org/2006/webapi/FileAPI/#FileReader-interface" rel="noreferrer">http://dev.w3.org/2006/webapi/FileAPI/#FileReader-interface</a></p> <p>Updated the answer and cleaned it up. Use the getSize function to check size or use getType function to check types. Added progressbar html and css code.</p> <pre><code>var Upload = function (file) { this.file = file; }; Upload.prototype.getType = function() { return this.file.type; }; Upload.prototype.getSize = function() { return this.file.size; }; Upload.prototype.getName = function() { return this.file.name; }; Upload.prototype.doUpload = function () { var that = this; var formData = new FormData(); // add assoc key values, this will be posts values formData.append("file", this.file, this.getName()); formData.append("upload_file", true); $.ajax({ type: "POST", url: "script", xhr: function () { var myXhr = $.ajaxSettings.xhr(); if (myXhr.upload) { myXhr.upload.addEventListener('progress', that.progressHandling, false); } return myXhr; }, success: function (data) { // your callback here }, error: function (error) { // handle error }, async: true, data: formData, cache: false, contentType: false, processData: false, timeout: 60000 }); }; Upload.prototype.progressHandling = function (event) { var percent = 0; var position = event.loaded || event.position; var total = event.total; var progress_bar_id = "#progress-wrp"; if (event.lengthComputable) { percent = Math.ceil(position / total * 100); } // update progressbars classes so it fits your code $(progress_bar_id + " .progress-bar").css("width", +percent + "%"); $(progress_bar_id + " .status").text(percent + "%"); }; </code></pre> <p>How to use the Upload class</p> <pre><code>//Change id to your id $("#ingredient_file").on("change", function (e) { var file = $(this)[0].files[0]; var upload = new Upload(file); // maby check size or type here with upload.getSize() and upload.getType() // execute upload upload.doUpload(); }); </code></pre> <p>Progressbar html code</p> <pre><code>&lt;div id="progress-wrp"&gt; &lt;div class="progress-bar"&gt;&lt;/div&gt; &lt;div class="status"&gt;0%&lt;/div&gt; &lt;/div&gt; </code></pre> <p>Progressbar css code</p> <pre class="lang-css prettyprint-override"><code>#progress-wrp { border: 1px solid #0099CC; padding: 1px; position: relative; height: 30px; border-radius: 3px; margin: 10px; text-align: left; background: #fff; box-shadow: inset 1px 3px 6px rgba(0, 0, 0, 0.12); } #progress-wrp .progress-bar { height: 100%; border-radius: 3px; background-color: #f39ac7; width: 0; box-shadow: inset 1px 1px 10px rgba(0, 0, 0, 0.11); } #progress-wrp .status { top: 3px; left: 50%; position: absolute; display: inline-block; color: #000000; } </code></pre>
    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.
    3. 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