使用uni-app实现点击上传,既可以上传视频,有可以上传图片,图片预览,删除图片和视频功能,最终效果如下。uni-app里面没有提供同时上传视频和图片这个插件,只能靠自己手写,

 1.页面布局

通过uni-app提供的标签,进行页面布局,这里就不多讲了,uni-app提供的有这个案例,可以直接把他们的样式拷贝过来修改一下就行。

<view class="uni-uploader-body">
<view class="uni-uploader__files">
<!-- 图片 -->
<block v-for="(image,index) in imageList" :key="index">
<view class="uni-uploader__file">
<view class="icon iconfont icon-cuo" @tap="delect(index)"></view>
<image class="uni-uploader__img" :src="data:image" :data-src="data:image" @tap="previewImage"></image>
</view>
</block>
<!-- 视频 -->
<view class="uni-uploader__file" v-if="src">
<view class="uploader_video">
<view class="icon iconfont icon-cuo" @tap="delectVideo"></view>
<video :src="src" class="video"></video>
</view>
</view>
<view class="uni-uploader__input-box" v-if="VideoOfImagesShow">
<view class="uni-uploader__input" @tap="chooseVideoImage"></view>
</view>
</view>
</view>

1.在data定义一些变量

data() {
return {
imageList:[],//图片
src:"",//视频存放
sourceTypeIndex: ,
sourceType: ['拍摄', '相册', '拍摄或相册'],
          VideoOfImagesShow:true,
cameraList: [{
value: 'back',
name: '后置摄像头',
checked: 'true'
},
{
value: 'front',
name: '前置摄像头'
},
],
}
},

3.通过使用uni-app提供的api​显示操作菜单,在methods写这个方法,通过判断来,选择的是图片还是视频,根据选择的tabindex选择,然后调用对应的方法即可

chooseVideoImage(){
uni.showActionSheet({
title:"选择上传类型",
itemList: ['图片','视频'],
success: (res) => {
console.log(res)
if(res.tapIndex == ){
this.chooseImages()
} else {
this.chooseVideo()
}
}
})
},

4.上传图片功能,也是通过uni-app提供的chooseImages来实现

chooseImages(){
// 上传图片
uni.chooseImage({
count: 4, //默认9
// sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: ['album','camera'], //从相册选择
success:(res)=> {
let igmFile = res.tempFilePaths;
uni.uploadFile({
url:this.config.fileUrl,
method:"POST",
header:{
'Authorization':'bearer '+ uni.getStorageSync('token'),
'Content-Type':'multipart/form-data'
},
filePath:igmFile[0],
name:'file',
success: (res) =>{
// let imgUrls = JSON.parse(res.data); //微信和头条支持
let imgUrls = res.data //百度支持
this.imagesUrlPath = this.imagesUrlPath.concat(imgUrls.result.filePath);
this.imageList = this.imageList.concat(imgUrls.result.filePath); //微信
if(this.imageList.length>=4) {
this.VideoOfImagesShow = false;
} else {
this.VideoOfImagesShow = true;
}
}
})
// this.imageList = this.imageList.concat(res.tempFilePaths) //头条
},
});
},

  

5.图片预览功能,urls必须要接受的是一个数组

previewImage: function(e) {
//预览图片
var current = e.target.dataset.src
uni.previewImage({
current: current,
urls: this.imageList
})
},

 6.点击图片删除功能,点击对应的图片,根据index索引值进行删除

delect(index){
uni.showModal({
title: "提示",
content: "是否要删除该图片",
success: (res) => {
if (res.confirm) {
this.imageList.splice(index, )
}
}
})
},

7.实现视频上传功能

chooseVideo(){
// 上传视频
uni.chooseVideo({
maxDuration:,
count: ,
camera: this.cameraList[this.cameraIndex].value,
sourceType: ['album'],
success: (responent) => {
let videoFile = responent.tempFilePath;
uni.uploadFile({
url:this.config.fileUrl,
method:"POST",
header:{
'Authorization':'bearer '+ uni.getStorageSync('token')
},
filePath:videoFile,
name:'file',
success: (res) => {
// let videoUrls = JSON.parse(res.data) //微信和头条支持
let videoUrls = res.data //百度支持
this.imagesUrlPath = this.imagesUrlPath.concat(videoUrls.result.filePath);
this.src = videoUrls.result.filePath; //微信
if(this.src) {
this.itemList = ['图片']
} else {
this.itemList = ['图片','视频']
} }
})
// this.src = responent.tempFilePath; //头条
}
})
},

8.点击视频删除功能

delectVideo(){
uni.showModal({
title:"提示",
content:"是否要删除此视频",
success:(res) =>{
if(res.confirm){
this.src = ''
}
}
})
},

最终代码

<template>
<view class="burst-wrap">
<view class="burst-wrap-bg">
<view>
<!-- 信息提交 -->
<view class="burst-info">
<view class="uni-uploader-body">
<view class="uni-uploader__files">
<!-- 图片 -->
<block v-for="(image,index) in imageList" :key="index">
<view class="uni-uploader__file">
<view class="icon iconfont icon-cuo" @tap="delect(index)"></view>
<image class="uni-uploader__img" :src="data:image" :data-src="data:image" @tap="previewImage"></image>
</view>
</block>
<!-- 视频 -->
<view class="uni-uploader__file" v-if="src">
<view class="uploader_video">
<view class="icon iconfont icon-cuo" @tap="delectVideo"></view>
<video :src="src" class="video"></video>
</view>
</view>
<view class="uni-uploader__input-box" v-if="VideoOfImagesShow">
<view class="uni-uploader__input" @tap="chooseVideoImage"></view>
</view>
</view>
</view> </view>
</view>
</view>
</view>
</template> <script>
var sourceType = [
['camera'],
['album'],
['camera', 'album']
]
export default {
data() {
return {
imageList:[],//图片
src:"",//视频存放
sourceTypeIndex: ,
checkedValue:true,
checkedIndex:,
sourceType: ['拍摄', '相册', '拍摄或相册'],
cameraList: [{
value: 'back',
name: '后置摄像头',
checked: 'true'
},
{
value: 'front',
name: '前置摄像头'
},
],
cameraIndex: ,
VideoOfImagesShow:true,
}
},
onUnload() {
this.src = '',
this.sourceTypeIndex = ,
this.sourceType = ['拍摄', '相册', '拍摄或相册'];
},
methods: {
chooseVideoImage(){
uni.showActionSheet({
title:"选择上传类型",
itemList: ['图片','视频'],
success: (res) => {
console.log(res)
if(res.tapIndex == ){
this.chooseImages()
} else {
this.chooseVideo()
}
}
})
},
chooseImages(){
// 上传图片
uni.chooseImage({
count: , //默认9
// sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: ['album','camera'], //从相册选择
success:(res)=> {
let igmFile = res.tempFilePaths;
uni.uploadFile({
url:this.config.fileUrl,
method:"POST",
header:{
'Authorization':'bearer '+ uni.getStorageSync('token'),
'Content-Type':'multipart/form-data'
},
filePath:igmFile[],
name:'file',
success: (res) =>{
// let imgUrls = JSON.parse(res.data); //微信和头条支持
let imgUrls = res.data //百度支持
this.imagesUrlPath = this.imagesUrlPath.concat(imgUrls.result.filePath);
this.imageList = this.imageList.concat(imgUrls.result.filePath); //微信
if(this.imageList.length>=) {
this.VideoOfImagesShow = false;
} else {
this.VideoOfImagesShow = true;
}
}
})
// this.imageList = this.imageList.concat(res.tempFilePaths) //头条
},
});
},
chooseVideo(){
// 上传视频
uni.chooseVideo({
maxDuration:,
count: ,
camera: this.cameraList[this.cameraIndex].value,
sourceType: ['album'],
success: (responent) => {
let videoFile = responent.tempFilePath;
uni.uploadFile({
url:this.config.fileUrl,
method:"POST",
header:{
'Authorization':'bearer '+ uni.getStorageSync('token')
},
filePath:videoFile,
name:'file',
success: (res) => {
// let videoUrls = JSON.parse(res.data) //微信和头条支持
let videoUrls = res.data //百度支持
this.imagesUrlPath = this.imagesUrlPath.concat(videoUrls.result.filePath);
this.src = videoUrls.result.filePath; //微信
if(this.src) {
this.itemList = ['图片']
} else {
this.itemList = ['图片','视频']
} }
})
// this.src = responent.tempFilePath; //头条
}
})
},
previewImage: function(e) {
//预览图片
var current = e.target.dataset.src
uni.previewImage({
current: current,
urls: this.imageList
})
},
delect(index){
uni.showModal({
title: "提示",
content: "是否要删除该图片",
success: (res) => {
if (res.confirm) {
this.imageList.splice(index, )
}
}
})
},
delectVideo(){
uni.showModal({
title:"提示",
content:"是否要删除此视频",
success:(res) =>{
if(res.confirm){
this.src = ''
}
}
})
}
}
}
</script> <style>
.burst-wrap{
width: %;
height: %;
}
/* .burst-wrap .burst-wrap-bg{
position: relative;
width: 100%;
height: 320upx;
background:linear-gradient(90deg,rgba(251,91,80,1) 0%,rgba(240,45,51,1) 100%);
border-bottom-right-radius: 80upx;
border-bottom-left-radius: 80upx;
} */
.burst-wrap .burst-wrap-bg>view{
width: %;
height: %;
margin: auto;
position: absolute;
top: 65upx;
left: ;
right: ;
} .form-item{
width: %;
}
.form-item textarea{
display: block;
height: 220upx;
width: %;
color: #AAAAAA;
font-size: 28upx;
}
.uni-uploader__file,.uploader_video{
position: relative;
z-index: ;
width: 200upx;
height: 200upx;
}
.uni-uploader__img {
width: 200upx;
height: 200upx;
}
.icon-cuo {
position: absolute;
right: ;
top: 5upx;
background: linear-gradient(90deg,rgba(,,,) %,rgba(,,,) %);
color: #FFFFFF;
z-index: ;
border-top-right-radius: 20upx;
border-bottom-left-radius: 20upx;
}
.video{
width: %;
height: %;
} .login-input-box{
position: relative;
border-bottom: 1upx solid #EEEEEE;
}
.login-input-box .forget,.login-input-box .phone{
position: absolute;
top: ;
height: %;
z-index: ;
}
.login-input-box .phone{
width: 100upx;
left: ;
color: #;
font-weight: bold;
}
.login-input-box .phone-input{
padding-left: 100upx;
}
.address-wrap,.open-info{
margin-top: 20upx;
}
.open-info>view:last-child{
font-size: 28upx;
color: #;
}
.address-wrap .address {
background: #F2F2F2;
border-radius: 40upx;
padding: 20upx;
}
.user-set-btn{
margin: 40upx;
background: linear-gradient(90deg,rgba(,,,) %,rgba(,,,) %);
color: #FFFFFF;
text-align: center;
height: 88upx;
line-height: 88upx;
}
</style>

以上都是实现这个功能的所有代码。

uni-app实现图片和视频上传功能的更多相关文章

  1. FileReader与URL.createObjectURL实现图片、视频上传前预览

    之前做图片.视频上传预览常用的方案是先把文件上传到服务器,等服务器返回文件的地址后,再把该地址字符串赋给img或video的src属性,这才实现所谓的文件预览.实际上这只是文件“上传后再预览”,这既浪 ...

  2. input实现图片或视频上传(样式+代码)

    背景:vue/element.ui 1..html: <div v-show="recordForm.resourceType==1"> <el-form-ite ...

  3. iView + vue-quill-editor 实现一个富文本编辑器(包含图片,视频上传)

    1. 引入插件(注意IE10以下不支持) npm install vue-quill-editor --savenpm install quill --save (Vue-Quill-Editor需要 ...

  4. (转)Android学习-使用Async-Http实现图片压缩并上传功能

    (转)Android学习-使用Async-Http实现图片压缩并上传功能 文章转载自:作者:RyaneLee链接:http://www.jianshu.com/p/940fc7ba39e1 让我头疼一 ...

  5. JSP+SpringMVC框架使用WebUploader插件实现注册时候头像图片的异步上传功能

    一.去官网下载webuploader文件上传插件 https://fex.baidu.com/webuploader/ 下载好后把它放到Javaweb项目的文件夹中(我放到了webcontent下面的 ...

  6. antdv的Upload组件实现前端压缩图片并自定义上传功能

    Ant Design of Vue的Upload组件有几个重要的api属性: beforeUpload: 上传文件之前的钩子函数,支持返回一个Promise对象. customRequest: 覆盖组 ...

  7. android之使用GridView+仿微信图片上传功能

    由于工作要求最近在使用GridView完成图片的批量上传功能,我的例子当中包含仿微信图片上传.拍照.本地选择.相片裁剪等功能,如果有需要的朋友可以看一下,希望我的实际经验能对您有所帮助. 直接上图,下 ...

  8. 【腾讯云的1001种玩法】 Laravel 整合微视频上传管理能力,轻松打造视频App后台

    版权声明:本文由白宦成原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/108597001488193402 来源:腾云阁 h ...

  9. Thinkphp5图片上传正常,音频和视频上传失败的原因及解决

    Thinkphp5图片上传正常,音频和视频上传失败的原因及解决 一.总结 一句话总结:php中默认限制了上传文件的大小为2M,查找错误的时候百度,且根据错误提示来查找错误. 我的实际问题是: 我的表单 ...

随机推荐

  1. 究极秒杀Loadrunner乱码

    Loadrunner乱码一击必杀 之前有介绍一些简单的针对Loadrunner脚本或者调试输出内容中乱码的一些设置,但是并没能完全解决一些小伙伴的问题,因为那些设置实在能力有限,还是有很多做不到的事情 ...

  2. call() 、 apply() 、bind()方法的作用和区别!

    从一开始,我是在书上看到关于bind().call() 和 apply(), 不过长久以来,在工作中与网上接触到了很多关于这三个方法的使用场景,对这三个方法也算是比较熟悉了.所以把他们的作用和区别简单 ...

  3. [ASP.NET Core 3框架揭秘] 异步线程无法使用IServiceProvider?

    标题反映的是上周五一个同事咨询我的问题,我觉得这是一个很好的问题.这个问题有助于我们深入理解依赖注入框架在ASP.NET Core中的应用,以及服务实例的生命周期. 一.问题重现 我们通过一个简单的实 ...

  4. 解决WebUploader 上传按钮按F12 才行的问题

    遇到了 WebUploader 插件的上传按钮点击无效(此时鼠标在按钮任何位置时,按钮都没变化).按F12 之后才有反应(此时鼠标在按钮任何位置时,按钮颜色都会变深) 的问题,网上查到一些答案,找到了 ...

  5. 深入浅出Spring(四)

    我们分别介绍了一下Spring框架的两个核心一个是IoC,一个是AOP.接下来我们来做一个Spring的实例. 为了更好的讲解Spring的相关内容,这次的博文会针对一个[添加用户]的实例,进行逐步的 ...

  6. Vue-Property-Decorator源码分析

    概述 vue-property-decorator是基于vue组织里vue-class-component所做的拓展,先来了解一下vue-class-component Vue-Class-Compo ...

  7. Win10无法安装.NET Framework3.5的解决办法

    诸位网友如果工作中使用WIN10遇到如图的这种问题,现将解决办法整理如下: 一.第一步就是修复系统:按“Windows+X”点击“Windows PowerShell(管理员)&命令提示符(管 ...

  8. 鲲鹏云实验-Python+Jupyter机器学习基础环境

    [摘要] 介绍Ubuntu 18.04环境下Python3常用科学计算和数据分析包(numpy, scipy, matplotlib, sklearn, pandas)的安装,以及Jupyter No ...

  9. FSM有限状态机 ---C#、Unity

    抽象类State public interface State//定义状态接口 { void Init();//初始化 int GetCurrentStateId();//返回当前状态Id void ...

  10. luogu P1754 球迷购票问题

    题目背景 盛况空前的足球赛即将举行.球赛门票售票处排起了球迷购票长龙. 按售票处规定,每位购票者限购一张门票,且每张票售价为50元.在排成长龙的球迷中有N个人手持面值50元的钱币,另有N个人手持面值1 ...