0

I have written below code to upload images using ajax without form tag. My basic requirement is that after clicking file input, the path of image should be saved in SESSION & once order is confirm the session get destroy and image are saved to folder.

<input id="file-input" name="file-input[]" multiple accept=".png, .PNG, .jpg, .JPG, .jpeg, .JPEG, .PDF, .pdf" type="file" />
$(document).ready(function() {
  $('input[type=file]').change(function() {
    var formData = new FormData();
    var files = this.files;
    for (var i = 0; i < this.files.length; i++) {
      formData.append('file', $('#file-input')[0].files[0]);
    }

    $.ajax({
      type: "POST",
      url: "ajax.php?type=uploadImgSession", // 
      data: formData,
      processData: false,
      contentType: false,
      success: function(msg) {
        alert(msg);
      },
      error: function() {
        alert("failure");
      }
    });
  })
});

My issue is that formData is coming through as blank every time. How can I rectify this?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

1 Answers1

1

As you are trying to upload files using Ajax, you can use jquery - $(form).ajaxSubmit() Ref link

Sirisha
  • 141
  • 4