Uploading files with AJAX

javascript ajax file upload example
ajax file upload jquery example
ajax file upload php
jquery ajax file upload multipart/form-data
ajax multipart/form-data file upload
xmlhttprequest upload file
two techniques for ajax file upload
jquery file upload

I am attempting to upload an image via AJAX using a simple HTML form including a input[type='file'] element, and formData(), however when I inspect formData() it appears to be empty, and nothing is being passed to the server.

jQuery

//Dynamically loaded form

$(document).on('submit', '.update-form', function(e) {
    e.preventDefault();
    var form = $(this);

    amendDatabase(form);
});

function amendDatabase(form) {
    var formData = new FormData(form);
    console.log(formData); //This produces 'formData: No properties'

    var url = 'file.php';

    request = $.ajax({
        url:            url,
        method:         'post',
        data:           formData,
        processData:    false,
        contentType:    false
    });

    request.done(function(data) {
        console.log(data); //This produces an empty/blank return
    });
}

file.php

function uploadFile() {
    echo 'formData was passed to server';
}

uploadFile();

HTML

<form class="update-form" id="add-file" enctype="multipart/form-data">
    <label for="image">Product image</label>
    <input type="file" id="image" name="image" multiple="multiple">
    <div class="hidden-holder">
        <input type="hidden" id="action" name="action" value="file">
    </div>
    <div class="submit-holder">
        <input type="submit">
    </div>
</form>

Direct file upload from ajax only works for new browser.insteat of using direct ajax use this plugin: http://malsup.com/jquery/form/

 $(document).ready(function() {
   $('#add-file').ajaxForm(function() { 
                alert("Upload Done!"); 
     }); 
});

you dont have to change anything of your html.

Ridiculously simple Ajax uploads with FormData, Use the JavaScript FormData object to make Ajax based file uploads ('upload' in xhr) && ('onprogress' in xhr.upload)); }; function  Learn Uploading Files with AJAX Selecting Files to Upload. The first thing you need to do is set up your HTML form Uploading Files to the Server. First of all you need to create three variables Browser Support. Browser support for the technologies used in this post is generally good.


const fileInput = document.getElementById('file');
fileInput.addEventListener('change', function() {
  let name = this.files[0].name;
  let extension = this.files[0].type;
  let date = new Date();
  getBase64(this.files[0], function(base64) {
    ajax(
      'POST',
      'Index.aspx/UploadFile',
      JSON.stringify({
        name: name,
        extension: extension,
        date: date,
        data: base64
      }),
      function() {
        console.log(JSON.parse(this.response));
        alert('Uploaded!');
      });
  });
});

function getBase64(file, call) {
  let fr = new FileReader();
  fr.readAsDataURL(file);
  fr.onload = function(data) {
    call(btoa(this.result));
  }
}

function ajax(method, url, data, call) {
  let request = new XMLHttpRequest();
  request.timeout = 10000;
  request.addEventListener('load', call);
  request.open(method, url);
  request.setRequestHeader('Content-Type', 'application/json');
  request.setRequestHeader('DataType', 'json');
  request.send(data);
}
<label>
    Select a file...
    <input id="file" name="file" type="file" />
</label>

<?php
    $name = $_POST['Name'];
    $data = $_POST['Data'];
    $extension = $_POST['Extension'];
    $date = $_POST['Date'];
    echo 'yep!'
?>

How to Upload File in AJAX? Step By Step Guide, Uploading a File. Important Steps to Upload Files in AJAX. There are a few crucial steps to note while uploading files through Ajax. It may seem  This HTML5 file uploader is particularly nifty — you can add files easily by dragging and dropping them or clicking. All files will be uploaded via AJAX or can be added within a form, and files can be renamed before uploading. A great, quick solution if that's what you're looking for!


you should append your file to form data :

// Create a formdata object and add the files
    var data = new FormData();
    $.each(files, function(key, value)
    {
        data.append(key, value);
    });

 $.ajax({
        url: 'submit.php?files',
        type: 'POST',
        data: data,
        cache: false,
        dataType: 'json',
        processData: false, // Don't process the files
        contentType: false, // Set content type to false as jQuery will tell the server its a query string request

Uploading Files with AJAX, In this post you'll learn how to upload files to a web server using AJAX. 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.


AJAX File Upload - Quick Tutorial & Time Saving Tips, Tutorial: AJAX file upload- a step by step guide, and how to do it with Cloudinary in under 5 minutes and the barebones way. We’ll be uploading files to a PHP page, upload.php. The page will handle both the Ajax upload requests and standard form POSTs when the user clicks “Upload Files”. Our JavaScript will ensure that only JPG images are uploaded which are smaller than 300,000 bytes — the value specified in MAX_FILE_SIZE.


Uploading Files With AJAX, All files will be uploaded via AJAX or can be added within a form, and files can be renamed before uploading. A great, quick solution if that's what  But using JQuery and Ajax we can accomplish this task without the entire page post back.In such cases, you can allow a user to select files using the FileUpload server control of ASP.NET and then upload those files to the server through an Ajax request to a generic handler. Requirement


AJAX Image and File Upload in PHP with jQuery, In just few easy steps, this tutorial on file and Image upload in PHP will be help you add an AJAX based file load in PHP componnet to your  basically async:false will do ajax request and stop executing further js code till the time request get complete, because upload file might take some time to upload to server. While cache will force browser to not cache uploaded data to get updated data in ajax request. Official Documentation here