Home >Web Front-end >JS Tutorial >Sample code to implement image and file upload in vue
Below I will share with you a sample code for uploading images and files in vue. It has a good reference value and I hope it will be helpful to everyone.
html page
<input type="file" value="" id="file" @change='onUpload'>//注意不能带括号
js code
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 ='<p 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></p>'; $("#refund_img").after(img_tpl); } }, error: function(err) { alert("网络错误"); } }); } }
Picture rendering
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
vue data control view source code analysis
A brief discussion of React high-order components
vue Sample code for developing a button component
The above is the detailed content of Sample code to implement image and file upload in vue. For more information, please follow other related articles on the PHP Chinese website!