Thursday, February 1, 2018

Laravel: Uploading of file to storage not working - development

Laravel: Uploading of file to storage not working

I’m trying to upload image to storage. I’m currently got this object but seems unfortunate to save on app/storage

IlluminateHttpUploadedFile Object
(
    [test:SymfonyComponentHttpFoundationFileUploadedFile:private] => 
    [originalName:SymfonyComponentHttpFoundationFileUploadedFile:private] => lamborghini_veneno_art_minimalism_99945_1366x768.jpg
    [mimeType:SymfonyComponentHttpFoundationFileUploadedFile:private] => image/jpeg
    [size:SymfonyComponentHttpFoundationFileUploadedFile:private] => 117303
    [error:SymfonyComponentHttpFoundationFileUploadedFile:private] => 0
    [hashName:protected] => 
    [pathName:SplFileInfo:private] => C:xampptmpphp82F7.tmp
    [fileName:SplFileInfo:private] => php82F7.tmp
)

Here is my code.

In profile.vue I have click event from input file.

onFilePicked(event){
    const files = event.target.files
    const data = new FormData();
    data.append('avatar', files[0]);
    this.$store.dispatch('uploadImage_profile',data)
        .then(response=>{

        })
        .catch(error=>{

        })
}

Then send using axios

    axios({
        url: '/prod/api/uploadImage_profile',
        method: 'post',
        data: obj
    })
    .then(response=>{
        if(response.status == 200){
            resolve(response.data)
        }
    })
    .catch(error=>{
        if(error.response){
            reject(error.response.data);
        }
    })

And my Controller.php

public function uploadImage_profile(Request $request){
    $response = [];
    //var_dump($request->file('avatar'));
    //$path = $request->file('avatar')->store('avatars');
  if($request->hasFile('avatar')){
    $file = $request->file('avatar');
    Storage::put('file.jpg', $file);
  }

  return response()->json($response);
}



from Laravel Questions and Answers https://laravelquestions.com/php/laravel-uploading-of-file-to-storage-not-working/
via Lzo Media

No comments:

Post a Comment