$request->file keeps returning tmp path

Viewed 805

I'm trying to upload a file using laravel and vue. When i console.log() the file, I get the picture below but in the controller, I'm receiving a tmp path only.

console log result

Vue

onFileChange(e) {
   ths.file = e.target.files[0];
   console.log(this.file)
},
submit(){
   let form= new FormData()
   form.append('file', this.file)
   axios.post('/api/archieve',form).then(res=>{
            //
   })
},

Controller

return $request->file //returns "C:\xampp\tmp\php5E67.tmp"

UPDATE I've checked using dd($request->file('file')); and it returns the below image but the realPath is wrong. The image is stored in a different folder on my pc.

enter image description here

1 Answers

Please try add header.

  onFileChange(e) {
    this.file = e.target.files[0];
    console.log(this.file)
  },
  submit() {
    let form= new FormData()
    form.append('file', this.file)
    axios.post('/api/archieve', form,  {
      headers: { 'Content-Type': 'multipart/form-data' }
    }).then(res=>{
      //
    })
  },

and in you controller, check

$request->file('file');

To store file

use Illuminate\Support\Facades\Storage;
....

$file = $request->file('file');

$path = 'my-uploads'; // path or folder where to save it
$storePath = Storage::put($path, $file);
$fileName = basename($storePath);  // generated file name

$filePath = $path . '/' . $fileName; // path of file in your storage
Related