記一次uni-app圖片上傳功能的實現app
upload() { var obj = {}; var thisa = this; uni.chooseImage({ sizeType: ['original', 'compressed'], //能夠指定是原圖仍是壓縮圖,默認兩者都有 sourceType: ['album'], //從相冊選擇 success: function(res) { console.log(JSON.stringify(res.tempFilePaths), 999999); let arr = []; let i = 0; for (var item of res.tempFiles) { //判斷圖片大小
if(item.size>10485760){ uni.showToast({ title: "圖片大小不能超過10M,請從新選擇", icon:"none" }); break; }; uni.uploadFile({ url: '域名', filePath: item.path, name: 'image', formData: { 'token': uni.getStorageSync("token") }, success: function(uploadFileRes) {var i = JSON.parse(uploadFileRes.data) thisa.img.push(i.path) thisa.host = i.server_url thisa.$emit("arr", thisa.img) }, error: function(a) { console.log(a,"失敗緣由"); } }); } }, error: function(e) { // console.log(e); } }); }
//刪除功能 i爲用戶要刪除第幾張圖片 shanchu(i) { var t = this uni.showModal({ title: '提示', content: '肯定刪除嗎?', success: function(res) { if (res.confirm) { t.img.splice(i, 1); t.$emit("arr", t.img) } } }); }