uniapp在H5下选取文件并上传到服务器

文件可以是图片、ppt、pdf等类型,主要借助于uni.chooseFile进行选择文件,然后借助uni.uploadFile进行上传到服务器。

1、html展示,提供一个上传按钮,再提供一个上传成功后展示容器

<view class="add-btn">
   <image src="../../static/images/evidence/add-icon.png" mode=""></image>
   <text class="text" @click="openFile">添加附件</text>
</view>

<scroll-view scroll-y >
	<view style="padding-bottom: 120rpx;">
	    <view class="add-image-item" v-for="(file, index) in currentFile">
	        <text class="add-image-item-name">{{(index + 1) + '.' + file.fileName}}</text>
	        <image 
	            class="add-image-item-delete" 
	            src="../../../static/image/file_del.png"
	            @click="currentFile.splice(index, 1)" />
	    </view>
	</view>
</scroll-view>

2、选取文件openFile()

// 打开文件选择器
openFile(){
    uni.chooseFile({
        count: 1, //默认100
        extension:['.zip','.doc','.xls','.pdf','docx','.rar','.7z','.jpg','.png','.jpeg'],
        success: (res) =>{
            console.log(res);
            if(res.tempFiles[0].size / 1024 / 1024 > 20){
                this.$refs.uToast.show({
                    title: '附件大小不能超过20M',
                    type: 'warning',
                })
                return;
            }
            this.resultPath(res.tempFilePaths[0],res.tempFiles[0].name);
        }
    });
},

3、上传选择的文件resultPath()

// 选取的文件路径获取后回调
resultPath(path,fileName) {
    console.log(path)
    console.log(fileName)
    uni.showLoading({
      title: '上传中...',
    });
    uni.uploadFile({
        url: base.baseUrl + '/upload', 
        filePath: path,
        header:{
            // "Authorization": "xxx",
            // 'content-type':'multipart/form-data; boundary=----WebKitFormBoundaryHEdN1AIjcdUkAaXM',
        },
        formData: {
            // 'user': 'test'
        },
        success: (uploadFileRes) => {
             let obj = JSON.parse(uploadFileRes.data);
             if(obj.code == 0){
                 this.getOssUrl(obj.tmpFileIds[obj.id],fileType,fileName);
             }else{
                 uni.showToast({
                     title:uploadFileRes.data.returnMessage,
                     icon:'none',
                     duration:1500
                 })
             }
         },
         fail:(err) =>{
             this.$refs.uToast.show({
                 title: '上传失败',
                 type: 'error',
             });
             uni.hideLoading();
         }
    });
},
  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现拍照上传或者选中图片上传,可以使用uniapp中的uni.chooseImage()和uni.chooseVideo()方法。这两个方法可以让用户选择或拍摄图片/视频并上传到服务器。 示例代码如下: ``` <template> <view> <button @tap="chooseImage">选择图片</button> <button @tap="chooseVideo">选择视频</button> </view> </template> <script> export default { methods: { // 选择图片 chooseImage() { uni.chooseImage({ count: 1, // 最多可以选择的图片数量 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: (res) => { const tempFilePaths = res.tempFilePaths // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片 console.log(tempFilePaths) // 将图片上传到服务器 }, fail: (err) => { console.log(err) } }) }, // 选择视频 chooseVideo() { uni.chooseVideo({ sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 maxDuration: 60, // 拍摄视频最长拍摄时间,单位秒。最长支持60秒 camera: 'back', // 指定使用前置或后置摄像头,默认为前后都有,即:['front', 'back'] success: (res) => { const tempFilePath = res.tempFilePath // 返回选定视频的临时文件路径 console.log(tempFilePath) // 将视频上传到服务器 }, fail: (err) => { console.log(err) } }) } } } </script> ``` 需要注意的是,选择图片和选择视频时需要用户授权,需要在manifest.json文件中配置权限。具体可以参考uniapp官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山为樽水为沼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值