Laravel-Vue: How to upload file
In developing application, file upload is the most common feature. We will disccus the process of uploading file at ease.
Objective:
- Upload file into the backend storage location.
Requirements:
- Laravel
- Vue 2
- Axios npm package
Procedures:
Vue
- We will create a Upload.vue file and set our upload form. We have a file input and set its ref "fileAttachUpload" attribute. The input file was not displayed on page. We will call this on the button which we will setup next.
- We will create a button on current vue page that will trigger the fileAttachUpload ref atrribute we previously defined.
- Let set our data
- Now, we go to our onFilesSelected method.
- The prepareData will set our uploaded files selected by user
Laravel
Route
Route::post("/upload", "UploadController@uploadFile);
Controller
php artisan make:controller UploadController
The uploadFile method will handle our files and store it to our "storage/app/public/files" directory.
public function uploadFile(Request $request)
{