如何将 Vue 图像对象发送到 Laravel 控制器?
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为
vue
将图像编码为base64
,因此解决方案是将其解码回laravel
。我必须将这些行添加到
MediaController.php
中:I figured that
vue
encodes the image tobase64
so the solution was to decode it back inlaravel
.I had to add these lines to
MediaController.php
: