laravel - 卡在通过 formData 上传图片
问题描述
我有一个页面,用户可以更改他的图片配置文件,但是当我做 a 时我被卡住了submitHandle
,因为当我尝试console.log(formData)
它时总是给我空的结果。
这是我的图片配置文件的输入文件
<input id="file-upload" type="file" name="fileUpload" class="d-none" @change="fileUpload">
<label for="file-upload">
<img class="ap-img__main rounded-circle wh-120 bg-lighter d-flex" :src="`/dashboard/img/author/profile/`+user.avatar"alt="profile img" v-if="!imagePreview">
<img class="ap-img__main rounded-circle wh-120 bg-lighter d-flex" :src="imagePreview" alt="profile img" v-if="imagePreview">
<span class="cross" id="remove_pro_pic">
<i class="fas fa-camera"></i>
</span>
</label>
这是方法
fileUpload(e) {
let selectedImage = e.target.files[0];
this.imageLocation = selectedImage;
console.log(this.imageLocation.name);
let reader = new FileReader();
reader.readAsDataURL(this.imageLocation)
reader.onload = e => {
this.imagePreview = e.target.result;
}
},
但是当我点击保存按钮保存文件时,按formData
方法,它会有空的结果。
这是按钮更新,
<div class="button-group d-flex pt-25 justify-content-start">
<button @click="submitHandle" class="btn btn-primary btn-default btn-squared text capitalize radius-md shadow2">Update details
</button>
</div>
这是submitHandle
方法,
submitHandle() {
e.preventDefault();
var formData = new FormData();
formData.append('image', this.imageLocation);
formData.append('name', this.user.name);
formData.append('username', this.user.username);
formData.append('email', this.user.email);
formData.append('phone', this.user.phone);
formData.append('gender', this.user.gender);
formData.append('birth', this.user.birth);
formData.append('bio', this.user.bio);
formData.append("facebook", this.user.facebook);
formData.append("instagram", this.user.instagram);
console.log(formData);
},
请任何人,可以帮我解决这个问题吗?非常感谢你。
解决方案
日志记录FormData
默认toString()
的FormData
不记录它的条目,所以它看起来只是FormData
空的。
记录条目的一种快速方法是包装FormData.prototype.entries
在一个数组中:
console.log(Array.from(formData.entries()))
const formData = new FormData();
formData.append('image', 'my image');
formData.append('name', 'my name');
formData.append('username', 'my username');
formData.append('email', 'my email');
formData.append('phone', 'my phone');
formData.append('gender', 'my gender');
formData.append('birth', 'my birth');
formData.append('bio', 'my bio');
formData.append("facebook", 'my facebook');
formData.append("instagram", 'my instagram');
console.log(Array.from(formData.entries()));
发送FormData
_axios
axios.patch
将FormData
实例作为第二个参数:
axios.patch(apiServerUrl, formData)
请注意,如果给定元素的所有s 都具有适当的属性集,您可以轻松地FormData
从给定元素创建 ,如下例所示:<form>
<input>
name
<template>
<form @submit.prevent="submitHandle">
<label>Profile Image <input type="file" name="image" autocomplete="photo" v-model="imageLocation"></label>
<label>Username <input type="text" name="username" autocomplete="username" v-model="user.username"></label>
<label>Email <input type="email" name="email" autocomplete="email" v-model="user.email"></label>
<label>Phone <input type="tel" name="phone" autocomplete="tel" v-model="user.phone"></label>
<label>Gender <input type="text" name="gender" autocomplete="sex" v-model="user.gender"></label>
<label>Birthdate <input type="date" name="birth" autocomplete="bday" v-model="user.birth"></label>
<label>Bio <textarea type="text" name="bio" v-model="user.bio"></textarea></label>
<label>Facebook <input type="text" name="facebook" v-model="user.facebook"></label>
<label>Instagram <input type="text" name="instagram" v-model="user.instagram"></label>
<button type="submit">Submit</button>
</form>
<pre>{{ response }}</pre>
</template>
<script>
import axios from 'axios'
export default {
data() {
return {
response: null,
imageLocation: null,
user: {
username: null,
email: null,
phone: null,
gender: null,
birth: null,
bio: null,
facebook: null,
instagram: null,
}
}
},
methods: {
submitHandle(e) {
const form = e.target
const formData = new FormData(form)
axios.patch('https://httpbin.org/patch', formData)
.then(resp => this.response = resp.data)
}
}
}
</script>
<style>
label {
display: block;
}
</style>
推荐阅读
- python - 为什么 lxml.etree.SubElement() 允许制作不可序列化的元素?
- ruby-on-rails - 如何更改此 ruby 匹配线以返回 true 或 false
- html - 为什么悬停状态不适用于 Safari?
- python - 从带有 URL 的 .CSV 文件下载图像时出现 HTTP 403 我该怎么办?
- git-bash - gpg:无法连接到代理:传递给 IPC 的值无效
- javascript - 从父母传下来后反应状态没有改变
- core-data - 如何在非 View Swift 文件中使用 ManagedObjectContext
- wordpress - 如何阻止访问 wp-content/uploads
- python - Python:设置x轴日期的间隔步长
- dataframe - 如何将 HTML 格式的文本数据读取到 Pandas 数据框?