Easy Ajax File Uploads With Formdata With Examples

Untung99 menawarkan beragam permainan yang menarik, termasuk slot online, poker, roulette, blackjack, dan taruhan olahraga langsung. Dengan koleksi permainan yang lengkap dan terus diperbarui, pemain memiliki banyak pilihan untuk menjaga kegembiraan mereka. Selain itu, Untung99 juga menyediakan bonus dan promosi menarik yang meningkatkan peluang kemenangan dan memberikan nilai tambah kepada pemain.

Berikut adalah artikel atau berita tentang Harian dengan judul Easy Ajax File Uploads With Formdata With Examples yang telah tayang di terimakasih telah menyimak. Bila ada masukan atau komplain mengenai artikel berikut silahkan hubungi email kami di, Terimakasih.

File uploads used to be difficult to implement for developers. Luckily, as web standards have advanced, so have file uploads. AJAX (XMLHttpRequests) now has ubiquitous browser support, and can be safely relied on to handle file uploads. And even better, the new FormData interface allows you to easily grab all of the form’s keys/values in just a couple of lines.

In this post, you’ll learn how to use current AJAX best practices to upload files to a server. The example below supports uploading multiple files in a single request, but the same technique can be used for single-file uploads as well.

Let’s begin!

Compatibility and Usage

Well actually, before we begin you should know that although AJAX and FormData are compatible with more than 95% of browsers, there’s a chance that you need to support the 5% of users who are using a legacy browser. If you’re not doing that, feel free to ignore this step. But if you need to support browsers like IE9, the best way to check for compatibility is to use object detection:

function supportAjaxUploadWithProgress() {
  return supportFileAPI() && supportAjaxUploadProgressEvents() && supportFormData();

  function supportFileAPI() {
    var fi = document.createElement('INPUT');
    fi.type = 'file';
    return 'files' in fi;

  function supportAjaxUploadProgressEvents() {
    var xhr = new XMLHttpRequest();
    return !! (xhr && ('upload' in xhr) && ('onprogress' in xhr.upload));

  function supportFormData() {
    return !! window.FormData;

If this function returns true, then you’re good. If not, the older methods can still be used such as traditional form submission.

As a reference, here are the browsers that currently support AJAX file uploads:

IE Firefox Chrome Safari Opera
10.0+ 4.0+ 7.0+ 5+ 12+

Now, let’s actually begin!

Uploading Files to the Server with AJAX

Now that you know that the client’s browser is compatible, the first thing you need to do is to create 3 HTML elements which will serve as the actual UI for the file upload. Note the multiple field on the input field, which allows the user to select multiple files by using the CTRL and SHIFT keys. The ID’s will allow you to reference these elements in the next step:

Also keep in mind here that the method and action fields for the form are actually not used if the form is sent using AJAX. They are defined there as a fallback in case the browser is not running JavaScript and needs to submit the form in the traditional fashion.

Next, you need to create three variables that hold references to the . , and