微信小程序实现上传图片

馋奶兔 提交于 2020-03-01 07:05:21
wx.chooseImage({

    count: 1,        //即一张

    sizeType: ['compressed'], // original 原图,compressed 压缩图,默认二者都有

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

    success: function (res) {
    
    that.setData({

    image:res.tempFilePaths,      //显示在前端界面

})

console.log(res.tempFilePaths[0])

    wx.uploadFile({

    url: url, //此处换上你的接口地址

    filePath: res.tempFilePaths[0],

    name: 'image',

    header: {

    "Content-Type": "multipart/form-data",

    },

formData: { id: 1},     //需要传的关于这个图片的信息,比如这个图片属于哪个用户的

success: function (res) {

    if (res.code == 200)

    {

    wx.showToast({
    
    title: '上传成功',
    
    icon: "success",

    duration: 1000

    })

    }

    },

fail: function (res) {

    wx.showToast({

    title: '网络异常,请稍后重试',

    icon: "none",

    duration: 1000

    })

    }

})

    }

})

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!