Element 單獨上傳圖片,不和form表單一起

1 Html

??????????<el-form-item?label="圖片"?prop="picUrl"?:label-width="formLabelWidth">

????????????<el-upload

??????????????v-model="form.picUrl"? ? // 表單提交對象

??????????????:action="actionImg"? // 圖片上傳地址,

??????????????list-type="picture-card"

??????????????:on-success="successImg"? //圖片上傳完成的回調(diào)

??????????????auto-upload

??????????????:headers="headers"? // 請求頭

??????????????ref="upload">

??????????????<i?class="el-icon-plus"></i>

????????????</el-upload>

??????????</el-form-item>

2 Js

??//?圖片上傳的?請求頭

??computed:?{

????headers()?{

??????return?{

????????Authorization:?window.sessionStorage.getItem("token")

??????};

????}

??}

????//?圖片上傳完成的回調(diào)

????successImg(response,?file,?fileList)?{

??????if?(response)?{? // 按各自后端需要來寫,圖片數(shù)據(jù)都在response里面

????????this.form.picUrl?=?response.data.url;??

????????this.form.remotePath?=?response.data.remotePath;?

??????}

????},

????// 確定按鈕

? ? addSubmit(from)?{

??????this.$refs[from].validate(async?valid?=>?{

????????if?(valid)?{?

??????????if?(this.form.picUrl?==?"")?{ //判斷圖片有沒有上傳成功

????????????this.$message.error("請選擇圖片");

????????????return?false;

??????????}

??????????//?成功?提交數(shù)據(jù)

??????????let?res?=?await?this.$axios.post("/product/save",?this.form);

??????????if?(res.data.code?!=?0)?{

????????????this.$message.error(res.data.message);

??????????}?else?if?(res.data.code?==?0)?{

????????????this.$message.success("新增成功");

??????????}

????????}

??????});

????},

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容