如何将 Vue 图像对象发送到 Laravel 控制器?

发布于 2025-01-13 11:12:59 字数 1771 浏览 1 评论 0原文

我正在尝试从 vue 前端发送一个 image 对象 到 laravel 控制器,以便它可以存储在公共存储中,但图像会被破坏。无法打开存储的图像文件并显示错误。

在此组件中:

<input v-if="!state.image" class="input-file" type="file" @change="onFileChange">

这些函数将输入图像转换为 base64 格式:

const createImage = (file) => {
    const reader = new FileReader();
    reader.onload = (e) => {
        state.image = e.target.result;
    };
    reader.readAsDataURL(file);
}

const onFileChange = (e) => {
    state.path = e.target.files[0].name
    const files = e.target.files;
    if (!files.length)
        return;
    createImage(files[0]);
}

并通过 ajax 请求将其发送

axios.post('/store', state)
    .then(res => {
        state.error = false
        router.push({name: 'artwork'})
    })
    .catch(err => {
        state.error = true
        setTimeout(() => state.error = false, 5000)
});

MediaController.php< /strong>:

<?php

namespace App\Http\Controllers;

use App\Models\Artwork;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;


class MediaController extends Controller
{
    public function store(Request $request)
    {
        $artwork = new Artwork;

        $artwork->name = $request->name;
        $artwork->path = '/storage/app/public/' . $request->path;
        $artwork->save();

        Storage::disk('public')->put($request->path, $request->image);

        return response()->json(null, 200);
    }
}

看起来 $request->image 中的 base64 图像数据 已损坏并保存在 /storage/app/public/1 中.jpg

如何在不破坏图像的情况下将图像从 vue 组件获取到控制器,然后再获取到公共存储?

I'm trying to send an image object from vue frontend to laravel controller, so that it can be stored in the public storage but the image gets destroyed. The stored image file can't be opened and shows an error.

In this component:

<input v-if="!state.image" class="input-file" type="file" @change="onFileChange">

these functions convert the input image to base64 format:

const createImage = (file) => {
    const reader = new FileReader();
    reader.onload = (e) => {
        state.image = e.target.result;
    };
    reader.readAsDataURL(file);
}

const onFileChange = (e) => {
    state.path = e.target.files[0].name
    const files = e.target.files;
    if (!files.length)
        return;
    createImage(files[0]);
}

and send it via ajax request:

axios.post('/store', state)
    .then(res => {
        state.error = false
        router.push({name: 'artwork'})
    })
    .catch(err => {
        state.error = true
        setTimeout(() => state.error = false, 5000)
});

to MediaController.php:

<?php

namespace App\Http\Controllers;

use App\Models\Artwork;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;


class MediaController extends Controller
{
    public function store(Request $request)
    {
        $artwork = new Artwork;

        $artwork->name = $request->name;
        $artwork->path = '/storage/app/public/' . $request->path;
        $artwork->save();

        Storage::disk('public')->put($request->path, $request->image);

        return response()->json(null, 200);
    }
}

It looks like the base64 image data in $request->image turns out broken and is saved in /storage/app/public/1.jpg.

How can I get the image from the vue component to the controller and then to the public storage without breaking it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年已逝 2025-01-20 11:13:00

我认为 vue 将图像编码为 base64,因此解决方案是将其解码回 laravel

我必须将这些行添加到 MediaController.php 中:

$image = str_replace('data:image/jpeg;base64,', '', $request->image);

Storage::disk('public')->put($request->path, base64_decode($image));

I figured that vue encodes the image to base64 so the solution was to decode it back in laravel.

I had to add these lines to MediaController.php:

$image = str_replace('data:image/jpeg;base64,', '', $request->image);

Storage::disk('public')->put($request->path, base64_decode($image));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文