我在使用Symfony 4的项目中安装了VueJS组件,但没有问题,但此刻我想上传图像。我遵循Laravel的以下参考:How to upload image from VueJS to Laravel with Axios?
我进入控制器,但是那是base 64中的值不仅仅到达控制台消息的地方。
代码:
//CargaFoto.vue
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<input type="file" name="image" @change="getImage" accept="image/*">
<button @click="updateAvatar">Subir Imagen</button>
</div>
</div>
</template>
<script>
import axios from 'axios'
export default {
name: "CargaFoto",
data() {
return {
msg: "Cargar Imagen de Perfil",
imagen: null
};
},
methods: {
getImage(event){
//Asignamos la imagen a nuestra data
this.imagen = event.target.files[0];
},
updateAvatar(){
//Creamos el formData
var data = new FormData();
data.append('avatar', this.imagen);
data.append('_method', 'POST');
//Enviamos la petición
axios.post('/usuario/jsonimagen',data)
.then(response => {
console.log(res)
})
}
</script>
这是控制器代码:
/**
* @return JsonResponse
* @Route("/jsonimagen", name="jsonimagen", methods="POST")
*/
public function jsonimagen(Request $request):JsonResponse
{
$data= $request->get("data");
return $this->json($data);
}
答案是null
我的疑问是如何将图像上传到本地服务器。
答案 0 :(得分:0)
您要以avatar
变量的形式发送文件内容,然后为什么要尝试获取请求的data
?
正确的格式为:
$avatar = $request->file('avatar');
此外,您可以在执行_method: 'POST'
时省略向发送的数据中添加axios.post
。
答案 1 :(得分:0)
基于link的注释的Fran,我可以看到标头丢失了,然后以已经定义的相同路径在Symfony中收集数据。
模板文件如下:
//CargaFoto.vue
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<div class="container">
<div class="large-12 medium-12 small-12 cell">
<label>File
<input type="file" id="file" ref="file" v-on:change="handleFileUpload()"/>
</label>
<button v-on:click="submitFile()">Submit</button>
</div>
</div>
</div>
</div>
</template>
<script>
import axios from 'axios'
export default {
name: "CargaFoto",
data() {
return {
msg: "Cargar Imagen de Perfil",
file: ''
//selectedFile: null
};
},
methods: {
submitFile(){
/*
Initialize the form data
*/
let formData = new FormData();
/*
Add the form data we need to submit
*/
formData.append('file', this.file);
/*
Make the request to the POST /single-file URL
*/
axios.post( '/usuario/jsonimagen',
formData,
{
headers: {
'Content-Type': 'multipart/form-data'
}
}
).then(function(){
console.log('SUCCESS!!');
})
.catch(function(){
console.log('FAILURE!!');
});
},
/*
Handles a change on the file upload
*/
handleFileUpload(){
this.file = this.$refs.file.files[0];
}
}
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
#fileInput {
display: none;
}
h1,
h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
.my-8 {
margin-top: 4rem;
margin-bottom: 4rem;
}
</style>
在请求数据的文件中,控制器为:
//src/UsuarioController.php
/**
* @return JsonResponse
* @Route("/jsonimagen", name="jsonimagen", methods="POST")
*/
public function jsonimagen(Request $request):JsonResponse
{
$usuario = $this->getUser();
$data = $request->files->get('file');
$fileName = $usuario.'.'.$data->guessExtension();
// moves the file to the directory where usuarios are stored
$data->move(
$this->getParameter('usuarios_directorio'),
$fileName
);
echo $data; exit;
return $this->json($data);
}
还要配置将图像加载到文件中的路径
//services.yaml
parameters:
locale: 'en'
usuarios_directorio: '%kernel.project_dir%/public/uploads/usuarios'
在这里您可以看到此捕获中加载的图像。