这期内容当中小编将会给大家带来有关如何在Vue 表单中对图片进行处理,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
<template> <div class="container"> <div id="nav"> <adminnav/> </div> <div id="create"> <h2>Create new post</h2> </div> <div id="post"> <body> <form> <label for="title">Title: </label> <textarea v-model=formdata.title rows="5" cols="60" name="title" placeholder="Enter text"> </textarea> <br/> <label for="body">Body: </label> <textarea v-model=formdata.body rows="5" cols="60" name="body" placeholder="Enter text"> </textarea> <br/> <label for="description">Description: </label> <textarea v-model=formdata.description rows="5" cols="60" name="description" placeholder="Enter text"> </textarea> <br/> <label for="snippet">Snippet: </label> <textarea v-model=formdata.snippet rows="5" cols="60" name="snippet" placeholder="Enter text"> </textarea> <br/> <label for="file">Upload photo: </label> <input class="form-control-file" type="file" accept="image/*" v-bind="formdata.photo" /> <br/> <input id="submit" type="submit" value="submit" @click.prevent="createPost()"/> </form> </body> </div> </div> </template> <script> import adminnav from '../components/adminnav.vue'; import PostService from '../service/PostService'; export default { name: 'createStory', components: { adminnav, }, data() { return { formdata: { title: '', body: '', description: '', snippet: '', photo: null, }, }; }, methods: { createPost() { console.log(this.formdata); /* eslint prefer-destructuring: 0 */ const formdata = this.formdata; PostService.createPost(formdata) .then(() => { console.log('success'); }); }, }, }; </script>
这是 POST 请求。
router.post("/add-story", upload.single('photo'), async(req, res) => { try{ let post = new Post(); post.title = req.body.title; post.description = req.body.description; post.photo = req.file.location; post.body = req.body.body; post.snippet = req.body.snippet; await post.save(); res.json({ status: true, message: "Successfully saved." }); } catch(err) { res.status(500).json({ success: false, message: err.message }); } });
让我们监视文件 <input>
中的 change
事件。这样可以确保每次用户的上传行为触发 updatePhoto
方法并把文件数据储存到 this.photo
。
<input type="file" accept="image/*" class="form-control-file" @change="updatePhoto($event.target.name, $event.target.files)" >
编码去收集所有的数据并发送请求
// vue组件的其他部分 data () { return { title: '', body: '', description: '', snippet: '', photo: {} // 储存文件数据 }; }, methods: { updatePhoto (files) { if (!files.length) return; // 存储文件数据 this.photo = { name: files[0].name, data: files[0] }; }, createPost() { let formData = new FormData(); formData.append('title', this.title); formData.append('body', this.body); formData.append('description', this.description); formData.append('snippet', this.snippet); formData.append('photo', this.photo.data, this.photo.name); PostService.createPost(formdata) .then(() => { console.log('success'); }); } } // vue组件的其他部分
上述就是小编为大家分享的如何在Vue 表单中对图片进行处理了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。