From 0263c0f838993d6b471772948fd060eef84ccae6 Mon Sep 17 00:00:00 2001 From: Torsten Date: Wed, 5 Feb 2020 17:04:01 +0100 Subject: [PATCH] Continue work on FileUpload. Created FileController --- motorradig/.idea/workspace.xml | 19 ++++++---- .../app/Http/Controllers/FileController.php | 37 +++++++++++++++++++ motorradig/public/js/app.js | 9 +++++ motorradig/resources/js/views/Upload.vue | 10 +++++ motorradig/routes/api.php | 1 + 5 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 motorradig/app/Http/Controllers/FileController.php diff --git a/motorradig/.idea/workspace.xml b/motorradig/.idea/workspace.xml index 48ef149..d6a608e 100644 --- a/motorradig/.idea/workspace.xml +++ b/motorradig/.idea/workspace.xml @@ -2,12 +2,11 @@ - + - - - + + @@ -263,10 +263,15 @@ - - + + + + + + + diff --git a/motorradig/app/Http/Controllers/FileController.php b/motorradig/app/Http/Controllers/FileController.php new file mode 100644 index 0000000..abcce67 --- /dev/null +++ b/motorradig/app/Http/Controllers/FileController.php @@ -0,0 +1,37 @@ +image); + + Log::info("exploded:"); + Log::info($exploded); + + $decoded = base64_decode($exploded[1]); + if(str_contains($exploded[0],’jpeg’)) { + $extension = 'jpg'; + } else { + $extension = 'png'; + } + $fileName=str_random().'.'.$extension; + $path = public_path().'/'.$fileName; + + file_put_contents($path, $decoded); + + + Log::info("Upload endpoint reached."); + Log::info("Filename:".$fileName); + + return response()->json([], 200); + } +} diff --git a/motorradig/public/js/app.js b/motorradig/public/js/app.js index 04e203d..185a1f3 100644 --- a/motorradig/public/js/app.js +++ b/motorradig/public/js/app.js @@ -2181,6 +2181,15 @@ __webpack_require__.r(__webpack_exports__); onFileSelected: function onFileSelected(event) { console.log("File changed:"); console.log(event.target.files[0].name); + var fd = new FormData(); + fd.append('image', event.target.files[0], event.target.files[0].name); + axios.post('http://localhost:8000/api/v1/auth/upload', fd, { + headers: { + 'Authorization': 'Bearer ' + this.$store.state.user_token + } + }).then(function (res) { + console.log(res); + }); } } }); diff --git a/motorradig/resources/js/views/Upload.vue b/motorradig/resources/js/views/Upload.vue index c4645a2..9f6819d 100644 --- a/motorradig/resources/js/views/Upload.vue +++ b/motorradig/resources/js/views/Upload.vue @@ -125,6 +125,16 @@ onFileSelected(event) { console.log("File changed:"); console.log(event.target.files[0].name); + const fd = new FormData(); + fd.append('image',event.target.files[0],event.target.files[0].name); + axios.post('http://localhost:8000/api/v1/auth/upload',fd,{ + headers: { + 'Authorization': 'Bearer ' + this.$store.state.user_token + } + }) + .then(res=>{ + console.log(res); + }) } } } diff --git a/motorradig/routes/api.php b/motorradig/routes/api.php index 19a5dd7..e183bfe 100644 --- a/motorradig/routes/api.php +++ b/motorradig/routes/api.php @@ -49,6 +49,7 @@ Route::prefix('v1')->group(function () { Route::post('set_role','AuthController@set_role'); Route::post('delete_user','AuthController@delete_user'); + Route::post('upload','FileController@upload'); }); }); });