您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在vue中使用input[type="file"]實現文件上傳功能,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
<template> <div id="my-careers"> <head-top id="header"> <i slot="left" class="iconfont icon-back"></i> <span slot="title">Apply Online</span> <i slot="right" class="icon-right fa"></i> </head-top> <div class="content"> <form @submit.prevent="onSubmit" id="fileForm" > <div class="form"> <h4>Customer Car Manager</h4> <ul class="form-group"> <li class="info-name"> <input type="text" v-focus :class="{errli:form.nameErr}" name="name" placeholder="Name" v-model="applyInfo.name" > <p class="errP" v-if="form.nameErr">{{form.nameMsg}}</p> </li> <li class="info-email"> <input type="text" :class="{errli:form.emailErr}" name="email" placeholder="Email Address" v-model="applyInfo.email" > <p class="errP" v-if="form.emailErr">{{form.emailMsg}}</p> </li> <li class="info-mobile"> <input type="number" :class="{errli:form.mobileErr}" name="mobile" placeholder="Mobile Number" v-model="applyInfo.mobile" > <p class="errP" v-if="form.mobileErr">{{form.mobileMsg}}</p> </li> <li class="info-location"> <input type="text" :class="{errli:form.locationErr}" name="location" placeholder="Current Location" v-model="applyInfo.location" > <p class="errP" v-if="form.locationErr">{{form.locationMsg}}</p> </li> <li class="reason-li" :class="{'info-reason':applyInfo.joinReason.split('').length >= 300}"> <textarea :class="{errli:form.joinReasonErr}" name="joinReason" v-model="applyInfo.joinReason" rows="5" cols="20" placeholder="Why do you want to join HappyEasy Go?" ></textarea> <span>{{wordNumber}}</span> <p class="errP" v-if="form.joinReasonErr">{{form.joinReasonMsg}}</p> </li> <li class="info-link"> <input type="text" :class="{errli:form.linkedinUrlErr}" name="linkedinUrl" placeholder="LinkedIn URL (Optional)" v-model="applyInfo.linkedinUrl" > <p class="errP" v-if="form.linkedinUrlErr" >{{form.linkedinUrlMsg}}</p> </li> <li class="info-file" > <span class="file-span" v-if="fileObj != null" v-show="isSelectFile">{{fileObj.name}}</span> <p v-show="!isSelectFile">Attach Resume</p> <p v-show="!isSelectFile">(Microsoft Word or PDF file is allowed (5MB))</p> <p><input type="file" id="upfile" accept="application/msword,application/pdf" name="upfile" @change="selectFile" ><span>Upload file</span> or drag and drop</p> </li> <li class="info-btns flex space-between" > <button @click="infoJustify" :class="{active:true}">Submit</button> </li> </ul> </div> </form> </div> <div class="career-cover" v-show="showDialog"></div> <div class="career-dialog" v-show="showDialog"> <div class="d-top"> Apply Online </div> <div class="d-center"> Thank you for applying to this job! </div> <div class="d-bottom"> <button @click="applySuccess">OK</button> </div> <img :src="dialogBg" alt=""> </div> </div> </template> ``` <script> import {Toast, Indicator} from 'mint-ui'; import { Reg } from '../../models/utils/Reg.js'; import headTop from "Components/head/header.vue"; import { DomainManager } from '../../request/DomainManager'; export default { components:{ headTop, }, data(){ return { arr:[], fileObj:null, showDialog:false, isSelectFile:false, dialogBg:require('../../assets/images/joinus/join-character.png'), applyInfo:{// form表單綁定的數據,linkedinUrl是可選參數 name:"", email:"", mobile:"", location:"", joinReason:"", linkedinUrl:"", positionId:null, departmentId:null, }, form:{// 對應input錯誤信息的設置 nameMsg:"Please enter a valid name",nameErr:false, emailMsg:"Please enter a valid Email Address",emailErr:false, mobileMsg:"Please enter a valid Mobile Number",mobileErr:false, locationMsg:"Please enter your current location",locationErr:false, joinReasonMsg:"Please write down your reason",joinReasonErr:false, linkedinUrlMsg:"",linkedinUrlErr:false, fileMsg:"Microsoft Word or PDF file is allowed (<5MB)", fileNull:"Please Upload your attach resume", submitFail:"Something's wrong, Please try it again", submitting:"Uploading...Please wait", }, } }, computed:{ wordNumber(){// 動態計算textarea的字數 let number = this.applyInfo.joinReason.split("").length; return (300 - number); } }, watch:{ applyInfo:{// 配合限制textarea的字數 handler:function(val,oldV){ if(this.applyInfo.joinReason.split('').length >= 300){ this.applyInfo.joinReason = this.applyInfo.joinReason.substr(0,300); } if(this.applyInfo.name != ""){ this.form.nameErr = false; } if(this.applyInfo.email != ""){ this.form.emailErr = false; } if(this.applyInfo.mobile != ""){ this.form.mobileErr = false; } if(this.applyInfo.location != ""){ this.form.locationErr = false; } if(this.applyInfo.joinReason != ""){ this.form.joinReasonErr = false; } }, deep:true } }, methods:{ selectFile(){ // 注意這里想要獲取formDate的數據格式,那么input必須加上name屬性,且name值對應綁定的v-model數據名字 let size; this.fileObj = document.getElementById("upfile").files[0];//獲取文件信息 if(this.fileObj){ // 原來的代碼,稍作調整 size = (this.fileObj.size / (1024 * 1024)).toFixed(2); let idx = this.fileObj.name.lastIndexOf("."); if (idx != -1){ let ext = this.fileObj.name.substr(idx+1).toUpperCase(); ext = ext.toLowerCase( ); if (ext != 'pdf' && ext != 'doc' && ext !='docx'){ Toast("You can upload.pdf,.doc,.docx files only.") if(this.arr.length >= 1){ this.fileObj = this.arr[0]; }else{ this.isSelectFile = false; } }else{ if(size >= 5){ Toast("Please select files within 5M") if(this.arr.length >= 1){ this.fileObj = this.arr[0]; }else{ this.isSelectFile = false; } }else{ this.arr[0] = this.fileObj; this.isSelectFile = true; } } } }else{// 當打開本地文件,點擊取消不選擇時,顯示已經選擇過的文件,如果沒有這個if則顯示沒有文件的狀態 if(this.arr.length >= 1){ this.fileObj = this.arr[0]; }else{ this.isSelectFile = false; } } }, infoJustify(){ // 判斷input的內容正確與否 if(this.applyInfo.name == ""){ this.form.nameErr = true; }else if(this.applyInfo.email == "" || !Reg.email.test(this.applyInfo.email)){ this.form.nameErr = false; this.form.emailErr = true; }else if(this.applyInfo.mobile == "" || !Reg.ChinaMobile.test(this.applyInfo.mobile)){ this.form.nameErr = false; this.form.emailErr = false; this.form.mobileErr = true; }else if(this.applyInfo.location == ""){ this.form.nameErr = false; this.form.emailErr = false; this.form.mobileErr = false; this.form.locationErr = true; }else if(this.applyInfo.joinReason == ""){ this.form.nameErr = false; this.form.emailErr = false; this.form.mobileErr = false; this.form.locationErr = false; this.form.joinReasonErr = true; }else if(this.fileObj == null){ this.form.nameErr = false; this.form.emailErr = false; this.form.mobileErr = false; this.form.locationErr = false; this.form.joinReasonErr = false; this.form.linkedinUrlErr = false; Toast({ message:"Please select the file", duration:1500 }); }else{ this.fileUpload(); } }, fileUpload(){ //這里是只上傳文件的步驟 // let formData = new FormData(); // formData.append("upfile",this.fileObj); // 這里上傳文件并且需要傳更多參數的時候 let fileForm = document.getElementById("fileForm");// 獲取整個form表單數據,記住input都需要name屬性 let url = DomainManager.saveCareer();// 上傳服務器接口 let params = this.$route.query; let formData = new FormData(fileForm); formData.append("positionId",params.positionId);// 這就是給formData添加新的表單數據的形式 formData.append("departmentId",params.departmentId); this.$axios({ url:url, method:"post", data:formData, headers:{"Content-Type":"multipart/form-data"}, }).then(res => { if(res.success){ this.showDialog = true; }else{ } }).catch(err =>{ console.log(err) }); }, applySuccess(){ this.showDialog = false; }, onSubmit(){ // 取消form表單的自動提交功能 return false; } }, directives:{ focus: { inserted: function (el) { el.focus() } } } } </script> <style lang="less" scoped> #my-careers{ .content{ padding: 0.5rem 0.2rem 0; text-align: left; h4{ font-size:0.16rem; padding: 0.15rem 0; } .form-group{ li{ position: relative; width:100%; margin-bottom:0.24rem; color:#999; display:flex; input,textarea{ width:93.5%; padding:0.09rem 3%; border:1px solid #ddd; border-radius:0.03rem; vertical-align: top; font-size: 0.14rem; } *::placeholder{ color:#999; } } .reason-li{ textarea{font-family: inherit;} span{ position:absolute; font-size:0.14rem; right: 0.05rem; bottom: 0; } } .info-reason{ textarea{resize: none;} } .info-file{ display:block; border:1px dashed #eee; font-size: 0.14rem; text-align: center; padding:0.13rem 0 0.1rem; margin-bottom:0.15rem; height:0.59rem; .file-span{ color:#999; background:#fff; padding:0.02rem 0.1rem; margin-bottom:0.05rem; border:1px solid #ddd; border-radius:3px; box-shadow:0 2px 5px #999; } p{ position: relative; line-height:0.2rem; color:#999; span{ color:#0b9d78; } input{ position: absolute; width:0.66rem; opacity: 0; display:inline-block; padding:0; border:0; } } } .info-btns{ margin-bottom:0; button{ width:2rem; padding:0.06rem 0; font-size: 0.16rem; color:#999; background: #eee; border-radius: 0.03rem; margin:auto; } .active{ color:#fff; background: #ffa000; } } .errli{ border:1px solid #d32f2f; } .errP{ position: absolute; font-size:0.14rem; color:#d32f2f; left: 0; bottom: -0.2rem; padding-left:3%; } } } .career-cover{ width: 100%; height: 100%; position:absolute; top: 0; left: 0; background: rgba(0, 0, 0, 0.5); z-index:10; } .career-dialog{ width:2.8rem; position: absolute; top: 35%; left: 0; right: 0; margin:auto; z-index:11; background:rgba(255, 255, 255, 1); padding:0.3rem 0.2rem; border-radius:5px; overflow: hidden; .d-top{ font-size: 0.14rem; color:#666; display:flex; align-items: center; font-weight: bold; } .d-top::after{ display:block; content:""; flex:1; margin-left:0.15rem; border-top:1px solid #999; } .d-center{ padding:0.3rem 0; color:#0b9d78; font-size: 0.16rem; font-weight: bold; } .d-bottom{ position: relative; height:0.3rem; margin-bottom:0.15rem; button{ position: absolute; width:2rem; padding:0.1rem 0; font-size: 0.16rem; color:#fff; background: #ffa000; border-radius:3px; left: 0; right: 0; margin:auto; z-index:9; } } img{ opacity: 0.2; position:absolute; width:1.8rem; top: 0.5rem; right: -0.1rem; z-index:8; } } } </style>
關于怎么在vue中使用input[type="file"]實現文件上傳功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。