小程序上传文件

这里用的是uniapp上传图片

<template>
<view class="upload" @click="selectType()">上传附件</view>
</template>
methods: {
selectType() {
    uni.chooseImage({
        count: 1, //默认9
        sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
        sourceType: ['album'], //从相册选择
        success: function(res) {
            that.uploadImages(res.tempFilePaths);
          }
    });
},
uploadImages(tempFilePaths) {
        let that = this;
        let tmpLen = tempFilePaths.length;
            let k = 0;
            let succ = 0;
            let fa = 0;
            function uploadDo() {
                uni.uploadFile({
                    url: "上传地址",
                    filePath: tempFilePaths[k],
                    name: 'files',
            formData:{
                file:tempFilePaths[k],
            },
                    success: function(res) {
            //成功处理
            }
                    },
                    fail: function() {
                        fa++;
                    },
                    complete: function() {
                        k++;
                        if (k < tmpLen) {
                            uploadDo();
                        } else {
                            showToast("上传成功")
                        }
                    }
                })
            }
            if (tmpLen > 0) {
                uploadDo();
            }
        },

}

注意:需要在微信后台设置上传域名,否则上线后会出现上传失败

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容