0

here's my html

<input type="file" class="form-control" name="file-name" id="file-id">

here's JS that is supposed to send file to api

var file = $('input#file-id').val();
$.post('my/url/that/works', {
    file: file,
    volume: volume
}).done(function () {
    //something
}).fail(function () {
    //something else
});

and finally my method in Laravel:

public function ajaxMethod(Request $request)
{
    if ($request->hasFile('file')) {
        echo "it does";
    }else{
        echo "it does not"; //     <- that happens
    }
    var_dump($request->getContent());//vardumps ('C:/fakepath/[correct_filename])
}

and the ajax call says that it does not has a file. I think that I upload only file name, but not the file. How can I change it?

Zbyszek Kisły
  • 2,110
  • 4
  • 26
  • 48

1 Answers1

0

To get file usei this:

var file = $('input#file-id').prop('files')[0];

instead of your code: var file = $('input#file-id').val();

Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33