uni-app上传图片视频

 

  html

<view class="imgItems">
<view class="grace-file-list" style="padding: 0;margin-bottom: 50upx">
<view class="items" v-for="(item, index) in fastImgLists" :key="index">
<view
class="video-bg"
v-if="item.type == 'video'"
style="position:relative;background-color: rgb(98,98,98);display: flex;justify-content: center;align-items: center;"
>
<image
v-show="fastImgLists[index].needload != 'true'"
:id="item.id"
:src="item.filePath"
mode="aspectFit"
:data-imgurl="item.filePath"
@tap="showVideo(index)"
style="width: 48upx;height: 48upx;"
></image>
</view>
<image v-else :src="item.filePath" mode="aspectFill" :data-imgurl="item.filePath" @tap="showImgs"></image>
<image
src="../../static/remove.png"
class="remove"
@tap="removeImg"
:id="'grace-items-img-' + index"
style="width: 38upx;height: 38upx;"
></image>
</view>
<view class="add-btn addImg" v-if="canAdd" @tap="actionSheetTap('fast')">
<image src="../../static/add-plus.png" :data-imgurl="imgSrc" mode=""></image>

定义

var maxNum = 6;
var sourceType = [['camera'], ['camera', 'album']];

data{

return{

canAdd: true,

cameraList: [
{
value: 'back',
name: '后置摄像头',
checked: 'true'
},
{
value: 'front',
name: '前置摄像头'
}
],
cameraIndex: 0,
sourceTypeIndex: 0
};

}

}

执行函数

//选择图片还是视频
actionSheetTap(type) {
console.log(type);
uni.showActionSheet({
title: '选择类型',
itemList: ['视频', '图片'],
success: e => {
console.log(e.tapIndex);
if (e.tapIndex == 0) {
this.chooseVideo(type);
} else {
this.addImg(type);
}
}
});
},

//选中图片
addImg: function(type) {
let _this = this;
var num = maxNum - _this.fastImgLists.length;
console.log(_this.fastImgLists.length);
if (num < 1) {
return false;
}
uni.chooseImage({
count: num,
sizeType: ['compressed'],
success: function(res) {
for (let i = 0; i < res.tempFilePaths.length; i++) {
let imgitem = {};
imgitem.type = 'img';
imgitem.id = res.tempFilePaths[i];
imgitem.filePath = res.tempFilePaths[i];
imgitem.needload = false;
_this.fastImgLists.push(imgitem);
}

//当添加的图片数量大于六时(v-for的list大于6),可添加图片的标志隐藏
if (_this.fastImgLists.length >= maxNum) {
_this.canAdd = false;
}
}
});
},

//选中视频
chooseVideo: function(type) {
let _this = this;
uni.chooseVideo({
camera: this.cameraList[this.cameraIndex].value,
sourceType: sourceType[this.sourceTypeIndex],
maxDuration: 10,
success: res => {
console.log(res.tempFilePath);
let imgitem = {};
imgitem.type = 'video';
imgitem.id = res.tempFilePath;
imgitem.filePath = '../../static/video.png';
imgitem.needload = false;
_this.fastImgLists.push(imgitem);

//v-for的list数量大于六时,添加标志隐藏
if (_this.fastImgLists.length >= maxNum) {
_this.canAdd = false;
}
}
});
},

//移除
removeImg: function(e) {
let _this = this;
console.log('removeImg');
var index = e.currentTarget.id.replace('grace-items-img-', '');
_this.fastImgLists.splice(index, 1);
if (_this.fastImgLists.length < maxNum) {
_this.canAdd = true;
}
},

//点击预览图片
showImgs: function(e) {
var currentImg = e.currentTarget.dataset.imgurl;
let urls = [];
urls.push(currentImg);
uni.previewImage({
urls: urls,
current: currentImg
});
},

//点击播放视频
showVideo(index) {
let file = this.fastImgLists[index].id;
console.log(file);
if (file.indexOf('http:') > -1) {
//未下载到本地
this.fastImgLists[index][subindex].needload = 'true';
this.fastImgLists[index][subindex].filePath = '';
this.downVideoFile(index, subindex, file);
} else {
//已下载到本地,跳播放页
console.log(file);
uni.navigateTo({
url: `../preview/previewVideo?file=${file}`
});
}
},

 


</view>
</view>

转载于:https://www.cnblogs.com/webzzc/p/11326120.html

### uni-app 实现视频图片上传功能 #### 定义必要的URL和变量 在 `data` 函数中定义两个重要的属性:一个是用于指定文件上传接口地址的 `uploadUrl`;另一个是用来保存已成功上传后的资源链接(无论是图片还是视频)的 `resourceUrl`。 ```javascript export default { data() { return { uploadUrl: 'https://example.com/api/upload', // 替换成实际的服务端API路径[^1] resourceUrl: '' // 存储返回来的媒体文件访问地址 } }, } ``` #### 使用组件完成选择与预览 通过 `<uni-file-picker>` 组件来让用户可以选择本地的照片库或相机拍摄新照片/录制视频,并支持即时预览所选内容。此组件提供了丰富的事件回调机制以便开发者能够监听用户的交互行为,比如选择了某个文件(`@select`)、删除了某项(`@delete`)或是整个表单提交完成后触发的成功响应处理函数(`@success`)等。 对于图片而言: ```html <template> <!-- ... --> <view class="uploader"> <uni-file-picker ref="imagePicker" fileMediatype="image" :limit="1" @select="onImageSelected" @success="onUploadSuccess"/> </view> </template> <script> methods:{ onImageSelected(e){ console.log('User selected image:', e.tempFilePaths); }, onUploadSuccess(res){ this.resourceUrl = res.data.url; console.log('Image uploaded successfully with URL:',this.resourceUrl); } } </script> ``` 针对视频的情况,则只需调整 `fileMediatype` 属性值为 `"video"` 即可满足需求: ```html <template> <!-- ... --> <view class="uploader"> <uni-file-picker ref="videoPicker" fileMediatype="video" :limit="1" @select="onVideoSelected" @success="onUploadSuccess"/> </view> </template> <script> // 方法同上... </script> ``` #### 处理上传逻辑 当用户完成了文件的选择操作后,可以通过调用组件实例上的 `.upload()` 方法启动真正的HTTP POST 请求向服务器发送数据包,在这之前记得设置好合适的请求头信息以及参数配置对象以适应目标服务端的要求。 考虑到不同类型的文件可能需要不同的处理方式,因此建议根据实际情况分别编写对应的业务逻辑代码片段来进行针对性的操作。 #### 性能优化措施 为了避免不必要的带宽消耗并提升用户体验,应当考虑采用诸如懒加载这样的策略仅当元素进入视口范围内再发起下载请求[^2]。另外,也可以尝试利用CDN分发静态资源或者启用浏览器级别的缓存机制等方式进一步改善整体表现效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值