HBuiderX uni-app 多图(本地)上传

废话不多说老规矩先上图

这些图片存的相对路径在项目底下

uni-app 多图上传跟普通的form表单上传还是有点区别 小程序不支持多图上传

上代码:前端代码跟效果图

先看效果图:

这里是整个图片不是有背景色注意,这里有功能有删除,放大,监听上传图片数量

前端代码:表单

        <view class="li">
            <view class="form_l">上传法人身份证:{{imageList2.length}}/2</view>
           <view class="uni-list list-pd">
               <view class="uni-list-cell cell-pd">
                   <view class="uni-uploader">
                       <view class="uni-uploader-body">
                           <view class="uni-uploader__files">
                               <block v-for="(image,index) in imageList2" :key="index">
                                <view class="deleteImage"><image src="/static/icon/delete1.png" class="delete"  @tap="delect(index,2)"></image></view>
                                   <view class="uni-uploader__file">
                                       <image class="uni-uploader__img" :src="image" :data-src="image" @tap="previewImage2"></image>
                                   </view>
                               </block>
                               <view class="uni-uploader__input-box">
                                   <view class="uni-uploader__input" @tap="chooseImage(2)"></view>
                               </view>
                           </view>
                       </view>
                   </view>
               </view>
           </view>

        </view>

js:

    export default {
        data() {
            return {
                imageList2: [],//这个是上传图片的集合
                count2:2,//这个是限制上传图片的个数
            }
        },

method:{

        // 选择图片
             chooseImage:  function(count) {
                let number=0;
                if(count==2){                        //这里加判断是我有多个上传事件
                    if(this.imageList2.length===2){
                        return;
                    }

                   //这个是监听选择图片的个数
                    number= this.imageList2.length+this.count2>this.count2?this.count2-this.imageList2.length : this.count2   
                }
     
                 uni.chooseImage({
                     sizeType: ['compressed'],
                     sourceType: ['album'],
                     count:number,
                     success: (res) => {
                        if(count==2){
                            this.imageList2 = this.imageList2.concat(res.tempFilePaths);
                        }
                     }
                 })
             },

//移除图片

    delect(index,count){
                uni.showModal({
                    title: '提示',
                    content: '是否删除该图片?',
                    success: (res) =>{
                        if (res.confirm) {
                  
                            if(count==2){
                                this.imageList2.splice(index, 1)
                            }
                         
                        }
                    }
                })
            },

    //预览图片也就是图片放大效果
            previewImage2: function(e) {
                var current = e.target.dataset.src
                uni.previewImage({
                    current: current,
                    urls: this.imageList2
                })
            },

}

//这里是上传操作 很关键

submit(){

                                                   array2=this.imageList2;//这是上传图片的集合
                                                    let imgArray2 = array2.map((value, index) => {
                                                        return {
                                                            name: "image" + index,//图片的下标元素
                                                            uri: value
                                                        }
                                                    })
                                                    uni.uploadFile({
                                                        url: url+"/CompanyController/upload?c_id="+this.c_id+"&type=3",//附带参数根据自己的需求
                                                        files: imgArray2, //必须制定files  规定
                                                        name: 'file',
                                                        formData: {
                                                            'totalNum': array2.length  //图片个数
                                                        },
                                                        success: (res) => {
                                                            let result = JSON.parse(res.data);
                                                            }
                                                    })
                                                }
                                            },
                                            fail: (res) => {
                                                this.$showToast.showToast("操作失败","none")    
                                            }
                                        })

}

}

前端代码基本结束了,接下来看后台(java)

@RequestMapping(value = "upload",headers = "content-type=multipart/form-data")
//headers = "content-type=multipart/form-data" 必须加 使用MultipartRequest 接收iamge
public @ResponseBody  Map<String,Object> upload(MultipartRequest request,HttpServletRequest req,int totalNum)throws Exception{
  if(totalNum()>0){
      List<MultipartFile> files=new ArrayList<MultipartFile>();
      for(int i=0;i<totalNum();i++){
          files.add(request.getFile("image"+i));
      }
     
  }else{
      map.put("code","imageIsNull");
  }
  return map;
}

后台输出效果:

到这里基本结束了 上传自己写普通的多图上传  有需要整个文件前留言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在uni-app上传本地图片到七牛云,可以按照以下步骤进行: 1. 在七牛云上创建一个存储空间,并获取该空间的Access Key和Secret Key。 2. 安装uni-app的官方插件uni-upload,并在项目根目录下的vue.config.js进行配置,添加以下代码: ``` const qiniuUploader = require('uniapp-qiniu-sdk'); const qiniuOptions = { region: 'your region', // 七牛云存储区域 uptokenURL: 'your uptokenURL', // 服务端提供获取上传凭证的接口 domain: 'your domain', // 七牛云存储空间绑定的域名 }; module.exports = { configureWebpack: { plugins: [ { apply: (compiler) => { compiler.hooks.afterEmit.tapPromise('afterEmit', (compilation) => { return new Promise((resolve, reject) => { qiniuUploader.upload(compilation.assets['your upload file name'].existsAt, 'your key', qiniuOptions, (res) => { console.log(res); resolve(); }, (error) => { console.log(error); reject(); }); }); }); }, }, ], }, }; ``` 3. 在需要上传图片的页面,添加以下代码: ``` <template> <view> <input type="file" @change="upload"> </view> </template> <script> export default { methods: { upload(event) { const file = event.target.files[0]; const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => { const base64Img = reader.result; qiniuUploader.upload(base64Img, (res) => { console.log(res); }, (error) => { console.log(error); }); }; }, }, }; </script> ``` 其,首先使用FileReader将本地图片转换为base64格式,然后调用qiniuUploader.upload函数上传图片。 以上就是在uni-app上传本地图片到七牛云的基本步骤,具体使用细节可以根据实际情况进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值