a1exlism
1/9/2018 - 6:59 AM

ajax_upload_image

<input id="avatar-upload" type="file" accept="image/*">
$('#avatar-uoload').on('change', function() {
	/* $() get arrays | files also is an array */
	var img = $(this)[0].files[0];
	var formData = new FormData();
	/* name, value [, filename] */
	formData.append('avatar', img, 'avatar' +
	 img.type.replace(/image\\/i, '');
	);
	formData.append('upload_file', true);

	$.ajax({
	   method: 'POST',
	   dataType: 'JSON',
	   url: _URL.upload,
	   data: formData,
	   async: true,
	   cache: false,
	   contentType: false,
	   processData: false,
	   success:function(data){
	       console.log("success");
	       console.log(data);
	   },
	   error: function(data){
	       console.log("error");
	       console.log(data);
	   }
	});
});