1

I have a form in datatable as follows.

<form method="post" enctype="multipart/form-data">
   <label for="file_'+row["course_id"]+'">
      <i class="far fa-2x fa-file-pdf f-gray"></i>
   </label>
   <input type="file" id="file_'+row["course_id"]+'">
</form>

This shows a pdf button inside datatable for file upload. Onclick it will show a file browser for file selection.

enter image description here

Using javascript's FormData() I am passing the file to Laravel controller.

$(document).on("change", "[id^=file_]", function(e) {
        var file_data = this.files[0];
        var form_data = new FormData();
        form_data.append("pdf_file", file_data);
        $.ajax({
            url: "/pdfUpload",
            cache: false,
            processData: false,
            data: form_data,
            type: 'post',
            success: function(data) {
                $('div.flash-message').html(data).fadeOut(5000);
            }
        });
    });

The request headers is as follows: enter image description here

But the following code says there is no file and is returning null.

public function upload(\Google_Service_Drive $service, Request $request){
        if ($request->hasFile('pdf_file')) {
            dump('yes');
        }else{
            dump('no');
        }
        dd($request->pdf_file);
        $data = file_get_contents(?)
}

Output:

enter image description here

Any help on what I'm missing here? Also, I need to get $data = file_get_contents(?) working. What should I pass here

Vamshi
  • 351
  • 2
  • 4
  • 21
  • 1
    use `dd($request->file('pdf_file'));`, also instead of doing echo 'Yes'; check if it is in bracket or not using `dump('Yes');`, Laravel provides `dd()` meaning dump & die and `dump()` for debugging purposes – bhucho Feb 03 '21 at 12:20
  • Be sure that send files with jquery is not enabled with all browser (https://stackoverflow.com/a/2320097/2084486) maybe that is your problem. Other solution create input type file with name `pdf_file[]` and use jquery to add file to this field. – Frédéric Klee Feb 03 '21 at 14:09
  • @FrédéricKlee true but that answer is 10 years old, there's probably <1% chance that OP is using one of those browser versions...it's unlikely to be relevant. Plus, the request header screenshot shows that the file is being sent anyway, so we already know that's not the problem. – ADyson Feb 03 '21 at 14:14
  • OK, sorry, you're right, have you checked basic php form information aka `$_FILES[]` – Frédéric Klee Feb 03 '21 at 14:21

0 Answers0