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('打开文档成功');
                            }
                        })
                    }
                })
            },
 

   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
uni-app中进行串口通信接收文件代码格式会依赖于你所选择的串口通信插件或原生插件的具体实现方式。以下是一个示例代码,供参考: ```javascript // 导入串口通信插件 import SerialPortPlugin from 'serial-port-plugin'; // 打开串口,配置参数 SerialPortPlugin.open({ port: '/dev/ttyS0', // 串口设备路径 baudRate: 9600, // 波特率 dataBits: 8, // 数据位 stopBits: 1, // 停止位 parity: 'none' // 校验位 }); // 监听串口数据接收事件 SerialPortPlugin.onDataReceived((data) => { // 在这里处理接收到的数据 // 根据文件输协议解析数据包,并组装成完整的文件 // 假设接收到的数据已经组装成完整的文件,保存到本地 uni.saveFile({ tempFilePath: 'data://' + filename, // 文件路径 success(res) { uni.showToast({ title: '文件保存成功', icon: 'success', duration: 2000 }); }, fail(err) { uni.showToast({ title: '文件保存失败', icon: 'none', duration: 2000 }); } }); }); ``` 上述代码中使用了一个名为`SerialPortPlugin`的串口通信插件,它提供了打开串口、配置参数、监听数据接收等功能。根据你所选择的插件或原生插件,代码的具体实现方式可能会有所不同。你需要根据插件的文档和示例代码来正确调用相关方法。 在数据接收的回调函数中,可以根据所选择的文件输协议解析数据包,并将数据包组装成完整的文件。这里的代码仅展示了文件保存的示例,你可以根据实际需求来处理接收到的数据。 需要注意的是,具体的串口通信插件和实现方式可能因设备和平台而异。因此,在使用插件之前,请确保该插件适用于你的设备和平台,并按照插件提供的文档进行正确配置和调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

std7879

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

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

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

打赏作者

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

抵扣说明:

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

余额充值