vue中实现图片和文件上传的示例代码
更新时间:2018年03月16日 09:49:55 投稿:jingxian
下面小编就为大家分享一篇在vue中实现图片和文件上传的示例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
html页面
<input type="file" value="" id="file" @change='onUpload'>//注意不能带括号
js代码
methods: { //上传图片 onUpload(e){ var formData = new FormData(); f ormData.append('file', e.target.files[0]); formData.append('type', 'test'); $.ajax({ url: '/ShopApi/util/upload.weixun',//这里是后台接口需要换掉 type: 'POST', dataType: 'json', cache: false, data: formData, processData: false, contentType: false, success: (res) => { if (res.code === 200) { var img_tpl ='<div class="picture" style="width:108px;float:left;"><img id="preview" src="'+后台返回的tu'pian路径+'" style="width:48px;height:48px;float:left;background-size:cover;"/><span class="r-span" onclick = "onDeletePicture()" style="color:#49BDCC;display:block;float:left;margin-left:10px;line-height:48px;">删除</span></div>'; $("#refund_img").after(img_tpl); } }, error: function(err) { alert("网络错误"); } }); } }
图片效果图
以上这篇vue中实现图片和文件上传的示例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
vue3+springboot部署到Windows服务器的详细步骤
这篇文章主要介绍了vue3+springboot部署到Windows服务器,配置Nginx时,因为现在是把vue前端交给了Nginx代理,所以这里的端口号不一定是我们在vue项目中设置的端口号,本文给大家介绍的非常详细,需要的朋友参考下吧2022-10-10
最新评论