如何在 Vue 表單中處理圖片
問題:
我在 Vue 中有一個 form 表單,用于上傳博客帖子,它有標(biāo)題、正文、描述、片段和圖片等范圍。所有的一切都是必需的。我在 Express 中設(shè)置了一個 API 來處理這個問題。我在 Postman 中測試正常,但是我不知道如何通過瀏覽器將文件發(fā)送給數(shù)據(jù)庫。我一直收到 500 錯誤,并且我將數(shù)據(jù)打印到控制臺,而圖片字段為空,所以我確信這就是問題所在,但我就是搞不清楚怎么辦。
這是我前端頁面的 form 表單:
<template> <div class="container"> <div id="nav"> <adminnav/> </div> <div id="create"> <h1>Create new post</h1> </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 }); } });
解決方法
讓我們監(jiān)視文件 <input>
中的 change
事件。這樣可以確保每次用戶的上傳行為觸發(fā) updatePhoto
方法并把文件數(shù)據(jù)儲存到 this.photo
。
<input type="file" accept="image/*" class="form-control-file" @change="updatePhoto($event.target.name, $event.target.files)" >
編碼去收集所有的數(shù)據(jù)并發(fā)送請求
// vue組件的其他部分 data () { return { title: '', body: '', description: '', snippet: '', photo: {} // 儲存文件數(shù)據(jù) }; }, methods: { updatePhoto (files) { if (!files.length) return; // 存儲文件數(shù)據(jù) 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 組件結(jié)構(gòu),我相信它與這個問題無關(guān),還有一些確保在啟動請求之前文件數(shù)據(jù)可用的檢查等等。這是一個關(guān)于如何獲取文件數(shù)據(jù)的想法,所以希望這個答案能啟發(fā)您。
以上就是如何在 Vue 表單中處理圖片的詳細(xì)內(nèi)容,更多關(guān)于Vue 表單中處理圖片的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue+ElementUI創(chuàng)建一個帶有進(jìn)度顯示的文件下載和打包組件功能
如何使用 Vue 創(chuàng)建一個帶有進(jìn)度顯示和打包功能的文件下載組件,我們探討了如何導(dǎo)入必要的包,構(gòu)建組件的基礎(chǔ)結(jié)構(gòu),實(shí)現(xiàn)文件下載與進(jìn)度顯示,以及如何將文件打包為 ZIP 格式供用戶下載2024-08-08關(guān)于Ant-Design-Vue快速上手指南+排坑
這篇文章主要介紹了關(guān)于Ant-Design-Vue快速上手指南+排坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06vue+element表格實(shí)現(xiàn)多層數(shù)據(jù)的嵌套方式
這篇文章主要介紹了vue+element表格實(shí)現(xiàn)多層數(shù)據(jù)的嵌套方式,具有很好的參考價值。希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09vue-router配合ElementUI實(shí)現(xiàn)導(dǎo)航的實(shí)例
下面小編就為大家分享一篇vue-router配合ElementUI實(shí)現(xiàn)導(dǎo)航的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02element基于el-form智能的FormSmart表單組件
本文主要介紹了element基于el-form智能的FormSmart表單組件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04