uniapp使用uview2上传图片功能

官网地址Upload 上传 | uView 2.0 - 全面兼容 nvue 的 uni-app 生态框架 - uni-app UI 框架

前提,需要下载vuew2插件

html 复制代码
<view class="upload">
    <view class="u-demo-block__content">
        <view class="u-page__upload-item">
            <u-upload :fileList="scoreFileList" @afterRead="afterRead" @delete="deletePic"
                multiple :maxCount="9" :previewFullImage="true"></u-upload>
        </view>
    </view>
</view>
javascript 复制代码
data {  
 scoreFileList: []
}
javascript 复制代码
     deletePic(event) {
            this.scoreFileList.splice(event.index, 1)
        },
        // 新增图片
        async afterRead(event) {
            let lists = [].concat(event.file);
            let fileListLen = this.scoreFileList.length;
            lists.map((item) => {
                this.scoreFileList.push({
                    ...item,
                    status: "uploading",
                    message: "上传中",
                });
            });
            for (let i = 0; i < lists.length; i++) {
                const result = await this.uploadFilePromise(lists[i].url);
                let item = this.scoreFileList[fileListLen];
                this.scoreFileList.splice(
                    fileListLen,
                    1,
                    Object.assign(item, {
                        status: "success",
                        message: "",
                        url: result,
                    })
                );
                fileListLen++;
            }
        },
        uploadFilePromise(url) {
            return new Promise((resolve, reject) => {
                uni.uploadFile({
                    url: 'http://www.example.com/upload', // 仅为示例,非真实的接口地址
                    filePath: url,
                    name: "file",
                    success: (uploadFileRes) => {
                        let res = JSON.parse(uploadFileRes.data);
                        resolve(res.message);
                    },
                    fail: (err) => {
                        console.log(err);
                    },
                });
            });
        },

获取图片地址,数组形式

"4213.png", "6816.png"

javascript 复制代码
 let detai_thumbs = this.scoreFileList.map((item) => item.url);
相关推荐
0wioiw012 分钟前
Flutter基础(前端教程④-组件拼接)
前端·flutter
花生侠36 分钟前
记录:前端项目使用pnpm+husky(v9)+commitlint,提交代码格式化校验
前端
一涯44 分钟前
Cursor操作面板改为垂直
前端
我要让全世界知道我很低调1 小时前
记一次 Vite 下的白屏优化
前端·css
1undefined21 小时前
element中的Table改造成虚拟列表,并封装成hooks
前端·javascript·vue.js
蓝倾1 小时前
淘宝批量获取商品SKU实战案例
前端·后端·api
comelong2 小时前
Docker容器启动postgres端口映射失败问题
前端
花海如潮淹2 小时前
硬件产品研发管理工具实战指南
前端·python
用户3802258598242 小时前
vue3源码解析:依赖收集
前端·vue.js