0

I have a simple section in which a user can upload a file, now I want to display success message after a file was successfully uploaded

HTML

 <form id="uploadform" action="upload.php" method="post" enctype="multipart/form-data">
              <label for="file"><span>Filename:</span></label>
              <input type="file" id="fileupload"  name="file" id="file" /> 
              <br />
              <input id="#submit_cf" type="submit" name="submit" class="btn btn-primary" value="Upload file" />
            </form>
            <span id="success_message"></span>

Ajax;

 function doSuccess(acton,message,disable)
        {
            $(acton).show();
            $(acton+' h2').text(message);
            $(disable).attr('disabled','disabled');
        }

    $('#uploadform').on('submit',function(e){
        // This is fine, it does prevent the form from submitting
        e.preventDefault();

        var form_data = $('#fileupload').prop('files')[0]; 
            console.log(form_data);
        // Run messaging
        doSuccess('#success_message','Processing, please wait...','#submit_cf');
        // Run ajax
        $.ajax({
            url : "upload.php",
            type: "POST",
            data: form_data,
            success: function(response) {
                $('#success_message ').text(response);
            }
        });
    });

on console console.log(form_data); return this enter image description here

Here is upload.php

<?php 

$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");

$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

if ((($_FILES["file"]["type"] == "video/mp4")
|| ($_FILES["file"]["type"] == "audio/mp3")
|| ($_FILES["file"]["type"] == "audio/wma")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg"))

&& ($_FILES["file"]["size"] < 5000000)
&& in_array($extension, $allowedExts))

  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";

      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

Now when I upload the file I get the following error

jquery.min.js:4 Uncaught TypeError: Illegal invocation

What is wrong with my code?

The Dead Man
  • 6,258
  • 28
  • 111
  • 193

1 Answers1

4

You're passing a file to data and jQuery is erroring while attempting to encode it as form data.

You should:

  • pass a real form data object instead
  • tell jQuery not to process the data
  • tell jQuery not to set a Content-Type (the browser will do it automatically with a FormData object)

Thus:

var form = $('#uploadform')[0];
var form_data = new FormData(form);

$.ajax({
    url : "upload.php",
    type: "POST",
    data: form_data,
    processData: false,
    contentType: false,
    success: function(response) {
        $('#success_message ').text(response);
    }
});
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335