Vue Version : 2.6.10
Laravel Version : 6.0
I am using this vue upload package and everything is ok on client side (at least I think so). But on the server side, where I am using the laravel, have some problem.
Here is my vue send method:
setImage: function (file) {
let formData = new FormData();
formData.append('file', file);
axios.post(upload_route, formData , {
headers: { 'Content-Type': 'multipart/form-data' }
})
.then(response => {
// upload successful
})
.catch(error => console.log(error));
},
And this is my server side method:
public function upload(Request $request){
$path = $request->file('file')->store('avatars');
return response('upload success' , 200);
}
When I upload the file to the server, it gives me this error:
"message": "Call to a member function store() on null",
The file object I am sending in the setImage
function is something like this (if I log it with console.log):
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE...
Generally no. Base64 will occupy more space than necessary to store those images. Depending on the size of your images and how frequently they're accessed, it is generally not an optimal stress to put on the database. You should store them on the file system or in something like Azure blob storage.
How to store base64 image using the Laravel's filesytem (File Storage) methods? base64_decode($encoded_image); but all of the Laravel's methods for storing files can accept either a Illuminate\Http\File or Illuminate\Http\UploadedFile instance.
'png'; \File::put(storage_path(). '/' . $imageName, base64_decode($image)); This the Code for covert any Base64 String to Image and store theme in a local path with file name.
I believe file
parameter on setImage
is not a File
object. So the $request->file('file')
is null
, because you attach a string
(base64), not a file.
You told us that output from console.log
is base64 path, then you need to convert that (base64) to file.
Since you're using Laravel, here is the technique:
use Illuminate\Support\Str;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\File\File;
.....
$base64File = $request->input('file');
// decode the base64 file
$fileData = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $base64File));
// save it to temporary dir first.
$tmpFilePath = sys_get_temp_dir() . '/' . Str::uuid()->toString();
file_put_contents($tmpFilePath, $fileData);
// this just to help us get file info.
$tmpFile = new File($tmpFilePath);
$file = new UploadedFile(
$tmpFile->getPathname(),
$tmpFile->getFilename(),
$tmpFile->getMimeType(),
0,
true // Mark it as test, since the file isn't from real HTTP POST.
);
$file->store('avatars');
Update
Since you're using vue-image-upload-resize, I check the documentation that it has built in function to change the output from base64 to blob, so you can just:
<image-uploader
...
output-format="blob"
... />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With