美文网首页
uniapp文件上传

uniapp文件上传

作者: 一名前端新手 | 来源:发表于2023-07-11 17:30 被阅读0次

uni.chooseImage({

    count: 1, //默认9

    sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有

    sourceType: ['album', 'camera'], //album 从相册选图,camera 使用相机

    success: function(img) {

        uni.showLoading({

            title: "上传中",

        })

        uni.uploadFile({

            url: globalConfig.baseUrl + '/v1/file/upload',

            filePath: img.tempFilePaths[0],

            header: {

                'token': uni.getStorageSync('token')

            },

            name: "file",

            formData: {

                     'file': img.tempFiles,

                    "file_type": 1, //其他表单字段,可根据需求添加

              },

                success: function(res) {

                    let data = JSON.parse(res.data)

                    uni.hideLoading()

                    _this.imgUrl = img.tempFilePaths[0] //拍照图片的路径

                },

                fail: function(err) {

                    uni.hideLoading()

                    _this.$refs.message.open()

                    _this.messageText = "上传失败"

                    _this.msgType = 'error'

                }

           })

    }

});

在选择图片后,通过uni.uploadFile函数将图片文件上传到服务器。其中:

url为服务器上传文件的接口地址

filePath为要上传的文件路径

name为服务端用于接受文件的字段名称

formData为要上传的其他表单数据,可自行添加

相关文章

网友评论

      本文标题:uniapp文件上传

      本文链接:https://www.haomeiwen.com/subject/vjcuudtx.html