uni-app上传图片和文件

如图所示:

 

 

 

上传图片,使用的是uni.chooseImage这个官方api,count 数量根据自己的需求来,我们是最多只能上传9张

复制代码
          uploadImgEvent(){
             uni.chooseImage({
                    count: 10 - this.uploadImgsList.length,
                    success: (res) => {
                    this.uploadImgsList.unshift(...res.tempFiles.map(item => ({
                        picturePath: item.path
                    })));
                    if(this.uploadImgsList.length == 10) this.uploadImgsList.pop();
                    }
            });
            },
复制代码
复制代码
uploads(){
    const _this = this;
return  Promise.all(_this.uploadImgsList.map(item=>_this.uploadImage(item.picturePath))).then(res=>{
            return res.map(item=>({
                 fileName:item.fileName,
                 filePath:item.url
            }))
   })
},
uploadImage(url) {
    return new Promise(async (resolve, reject) => {  
        uni.uploadFile({
            url: await getUploadUrl(), //后台地址
            filePath: url,
            name: 'file',
            success: (uploadFileRes) => {
                resolve(JSON.parse(uploadFileRes.data)); 
            }
        })
    })
},
复制代码

 



提交给后代的数据

//提交
async submitFlood(){
  let photoList = await this.uploads();
}
打印photoList如图:

 

 

 

上传文件使用的LFile 这个插件 https://ext.dcloud.net.cn/plugin?id=4109

根据官网案例来

复制代码
 
         //上传附件
            uploadFile() {
                const that = this;
                if(that.imgUploadList.length >= 9){
                    this.$mHelper.toast('最多上传9张')
                    return;
                }
                that.$refs.lFile.upload({
                        // #ifdef APP-PLUS
                        currentWebview: that.$mp.page.$getAppWebview(),
                        // #endif
                        url: 'xxxxxx', //你的上传附件接口地址
                        name: 'file'
                        },
                    });
            },
复制代码
复制代码
         //上传成功
            upSuccess(res) {
                let url = res.root.url;
                let name = res.root.originalName;
                let fileType = this.isFileType(res.root.type);
                let size = res.root.size;
                if(fileType == 'image'){
                    this.imgUploadList.push({url,name,fileType,size});
                }else{
                    this.fileUploadList.push({url,name,fileType,size})
                }
            },
         //根据文件后缀名来判断,展示对应的图标
        isImage(type){
                return ['png','jpg','jpeg','gif','svg'].includes(type.toLowerCase());
            },
            isDocx(type){
                return ['doc','docx'].includes(type.toLowerCase());
            },
            isXsls(type){
                return ['xsls','xsl'].includes(type.toLowerCase());
            },
            isText(type){
                return ['text','txt'].includes(type.toLowerCase());
            },
            isFileType(type){
                if(this.isImage(type)) return 'image';
                if(this.isXsls(type)) return 'excel';
                if(type == 'pdf') return 'pdf';
                if(this.isDocx(type)) return 'word';
                if(this.isText(type)) return "text";
                // return "#icon-file-b--6";
            },
           //文件预览
            previewFile(item){
                uni.downloadFile({
                    url: item.url,
                    success: (res) => {
                        let filePath = res.tempFilePath;
                        uni.openDocument({
                            filePath: filePath,
                            success: (res) => {
                                console.log('打开文档成功');
                            }
                        })
                    }
                })
            },
复制代码

 



posted @   喆星高照  阅读(6253)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示