Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FormData append not working

I wrote this to upload an image to my local Apache webserver using input element in HTML. The file is logged as not empty, but why is the form_data completely empty?

$('#upload-image').change(function(e){     var file = e.target.files[0];     var imageType = /image.*/;     if (!file.type.match(imageType))         return;     console.log(file);     var form_data = new FormData();     form_data.append('file', file);     console.log(form_data);     $.ajax({         url: 'http://localhost/upload.php',         cache: false,         contentType: false,         processData: false,         data: form_data,         type: 'POST',         success: function(response){             console.log(response);         },         error: function(error){             console.log(error);         }     });  }); 

This is my upload.php on local webserver

<?php     header('Access-Control-Allow-Origin: *');     if ( 0 < $_FILES['file']['error'] ) {         echo 'Error: ' . $_FILES['file']['error'] . '<br>';     }     else {         move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);         $target_path = $_SERVER['DOCUMENT_ROOT'] . "/uploads/" . $_FILES['file']['name'];         echo $target_path;     } ?> 

The console.log(response) logs all the code lines of PHP file, instead of the returned result of echo

like image 372
necroface Avatar asked Oct 15 '16 18:10

necroface


People also ask

How do I append in FormData?

append() The append() method of the FormData interface appends a new value onto an existing key inside a FormData object, or adds the key if it does not already exist.

Can I append object in FormData?

Yes you can, you can append to formData objects.

Why FormData is empty in jquery?

FormData wil support multi file upload. Should work great! NOTE: You may find that the FILES array is empty on server side page - In this case you need to make sure your server configuration allows file uploads, size limit of file upload is enough, post time is enough etc....

Can I append an array to FormData in JavaScript?

To append array to FormData and send via Ajax with JavaScript, we can use the spread operator. const formData = new FormData(); const arr = ["this", "is", "an", "array"]; for (const a of arr) { formData. append("arr[]", a); } console.


1 Answers

When logging a formData object with just console.log(formData) it always returns empty, as you can't log formData.

If you just have to log it before sending it, you can use entries() to get the entries in the formData object

$('#upload-image').change(function(e) {     var file = e.target.files[0];     var imageType = /image.*/;      if (!file.type.match(imageType)) return;      var form_data = new FormData();     form_data.append('file', file);      for (var key of form_data.entries()) {         console.log(key[0] + ', ' + key[1]);     }      $.ajax({         url: 'http://localhost/upload.php',         cache: false,         contentType: false,         processData: false,         data: form_data,         type: 'POST',         success: function(response) {             console.log(response);         },         error: function(error) {             console.log(error);         }     });  }); 
like image 88
adeneo Avatar answered Oct 10 '22 06:10

adeneo