zoukankan      html  css  js  c++  java
  • demo_03 云数据库文件操作

    <template>
        <view>
            首页
            <button type="primary" @click="getList">调用get_list云函数</button>
            <button type="primary" @click="uploadImg">上传图片</button>
            <button type="primary" @click="deleteImg">删除图片</button>
        </view>
    </template>

    <script>
        export default {
            data() {
                return {
                    title: 'Hello'
                }
            },
            onLoad() {
                console.log("页面加载:首页")
            },
            // tarbar 点击触发 可以做页面数据重新渲染的动作
            onTabItemTap(e) {
                console.log(e);
            },
            methods: {
                getList(){
                    uniCloud.callFunction({
                        name:"get_list",
                        data:{
                            // 请求name 为 name1 的数据
                            name: 'name1'
                        },
                        success(res) {
                            console.log(res);
                        },
                        fail(err) {
                            console.log(err)
                        }
                    })
                },
                uploadImg() {
                    new Promise((resolve, reject) => {
                        uni.chooseImage({
                            count: 1,
                            success: res => {
                                const path = res.tempFilePaths[0]
                                console.log('path是:' + path)
                                let ext
                                // #ifdef H5
                                ext = res.tempFiles[0].name.split('.').pop()
                                // #endif
                                // #ifndef H5
                                // 字节跳动小程序ios端选择文件会带query
                                ext = res.tempFilePaths[0].split('?')[0].split('.').pop()
                                // #endif
                                // 图片后缀
                                console.log('ext是:' + ext)
                                const options = {
                                    filePath: path,
                                    cloudPath: Date.now() + '.' + ext
                                }
                                resolve(options)
                            },
                            fail: () => {
                                reject(new Error('Fail_Cancel'))
                            }
                        })
                    }).then((options) => {
                        uni.showLoading({
                            title: '文件上传中...'
                        })
                        return uniCloud.uploadFile({
                            ...options,
                            onUploadProgress(e) {
                                console.log(e)
                            }
                        })
                    }).then(res => {
                        uni.hideLoading()
                        console.log(res);
                        uni.showModal({
                            content: '图片上传成功,fileId为:' + res.fileID,
                            showCancel: false
                        })
                    }).catch((err) => {
                        uni.hideLoading()
                        console.log(err);
                        if (err.message !== 'Fail_Cancel') {
                            uni.showModal({
                                content: `图片上传失败,错误信息为:${err.message}`,
                                showCancel: false
                            })
                        }
                    })
                },
                deleteImg(){
                    uniCloud.deleteFile({
                        fileList:['https://vkceyugu.cdn.bspapp.com/VKCEYUGU-zk-demo/fec169cc-d907-4392-b834-c31da3f6f662.png'],
                        success(res) {
                            console.log(res)
                        },
                        fail(err) {
                            console.log(err)
                        }
                    })
                }
            }
        }
    </script>

    <style>
        
    </style>
  • 相关阅读:
    赴美工作常识(Part 4
    赴美工作常识(Part 3
    50 年前的黑客黑什么?
    孩子王?有孩子气才能为王?
    赴美工作常识(Part 2
    UE4 Keynote 1
    Go Programming Language 3
    Go Programming Language 2
    Go Programming Language
    Lua语法要点2
  • 原文地址:https://www.cnblogs.com/luwei0915/p/13335649.html
Copyright © 2011-2022 走看看