1. 程式人生 > 其它 >uniapp 小程式上傳圖片

uniapp 小程式上傳圖片

data(){
return{
imgList:[],
imgArr:"",
pingjiaSrc:this.adminIp + "content/image/pingjia.png",
}
},
methods:{
   ChooseImage() {
        var _this = this;
          uni.chooseImage({
             count: 4, //預設9
             sizeType: ['original', 'compressed'], //可以指定是原圖還是壓縮圖,預設二者都有
             sourceType: ['
album', 'camera'], //從相簿選擇和拍照 success: (res) => { var tempFilePaths = res.tempFilePaths; if (_this.imgList.length+tempFilePaths.length > _this.imgMaxNum) { uni.showToast({ title: '超過上傳圖片的最大數量', icon:
'none' }) } else { if (_this.imgList.length != 0) { _this.imgList = _this.imgList.concat(res.tempFilePaths); } else { _this.imgList = res.tempFilePaths; }
for (var i = 0; i < tempFilePaths.length; i++) { let timestamp = new Date().getTime(); let enMsg = hex_md5('@' + hex_md5('@' + timestamp + '~') + '~'); uni.uploadFile({ url: this.adminIp + 'Interface/Interface/UploadImg',//上傳的伺服器地址 filePath: tempFilePaths[i], method: 'POST', name: "file", formData: { timestamp:timestamp, enMsg:enMsg }, success: res => { let img = JSON.parse(res.data).data _this.imgArr +=img + "," console.log("上傳圖片",_this.imgArr); } }); } } } }); }, //檢視圖片 lookImg: function(val) { const urls = this.imgList const current = val uni.previewImage({ urls, current }) }, delect(index){ uni.showModal({ title: "提示", content: "是否要刪除該圖片", success: (res) => { if (res.confirm) { this.imgList.splice(index, 1) } } }) }, }