jQuery AJAX POST File

Example how to POST a file

by a13xs

HTML

<form>
  <input type="file">
</form>

<button>
  Upload
</button>

JavaScript

$('button').click(function() {
  var formData = new FormData();
  var file = $('input').get(0).files[0];
  formData.append('file', file, encodeURI(file.name));
  $.ajax({
    url: 'http://www.crm.prokachu.com/api/files', // point to server-side PHP script 
    dataType: 'text',  // what to expect back from the PHP script, if anything
    cache: false,
    contentType: false,
    processData: false,
    data: formData,                         
    type: 'post',
    success: function(response){
    	console.log(response);
    }
  });
})