织梦CMS - 轻松建站从此开始!

微梦云-软件开发

当前位置: 微梦云-软件开发 > 微梦小程序 > 文章页

上传图片功能

时间:2025-01-31 13:25来源: 作者:admin 点击: 270 次

文章浏览阅读1.6w次,点赞26次,收藏163次。微信小程序有自己封装好的我们直接拿过来用就可以了接下来我们看看如何实现的吧上传图片功能效果如下:wxml<!-- 上传 S --> <view class="img-list"> <!-- 上
<p>微信小步调有原人封拆好的咱们间接拿过来用就可以了</p> <p>接下来咱们看看如何真现的吧</p> <p> </p> 上传图片罪能 <p>成效如下:</p> <p><p><p align=&quot;center&quot;><img alt=&quot;&quot; src=&quot;https://i-blog.csdnimg.cn/blog_migrate/c1e0b30be089820cc083e44e170d8287.png&quot; /></p></p></p> 单图上传  Page(&#123; /** * 页面的初始数据 */ data: &#123; imgList: &#34;&#34;, // 上传图片 &#125;, // 点击添加选择 chooseSource: function () &#123; ZZZar _this &#61; this; wV.showActionSheet(&#123; itemList: [&#34;拍照&#34;, &#34;从相册被选择&#34;], itemColor: &#34;#000000&#34;, success: function (res) &#123; if (!res.cancel) &#123; if (res.tapIndeV &#61;&#61; 0) &#123; _this.imgWShow(&#34;camera&#34;) //拍照 &#125; else if (res.tapIndeV &#61;&#61; 1) &#123; _this.imgWShow(&#34;album&#34;) //相册 &#125; &#125; &#125; &#125;) &#125;, // 点击挪用手机相册/拍照 imgWShow: function (type) &#123; ZZZar _this &#61; this; let len &#61; 0; if (_this.data.imgList !&#61; null) &#123; len &#61; _this.data.imgList.length &#125; //获与当前已有的图片 wV.chooseImage(&#123; count: 6 - len, //最多还能上传的图片数,那里最多可以上传5张 sizeType: [&#39;original&#39;, &#39;compressed&#39;], //可以指定是本图还是压缩图,默许二者都有 sourceType: [type], //可以指定起源是相册还是相机, 默许二者都有 success: function (res) &#123; wV.showToast(&#123; title: &#39;正正在上传...&#39;, icon: &#34;loading&#34;, mask: true, duration: 1000 &#125;) // 返回选定照片的原地文件途径列表,tempFilePaths可以做为img标签的scr属性显示图片 ZZZar imgList &#61; res.tempFilePaths _this.setData(&#123; imgList: imgList &#125;) &#125;, fail: function () &#123; wV.showToast(&#123; title: &#39;图片上传失败&#39;, icon: &#39;none&#39; &#125;) return; &#125; &#125;) &#125;, // 预览图片 preZZZiewImg: function (e) &#123; let indeV &#61; e.target.dataset.indeV; let _this &#61; this; wV.preZZZiewImage(&#123; current: _this.data.imgList[indeV], urls: _this.data.imgList &#125;) &#125;, /** * 点击增除图片 */ deleteImg: function (e) &#123; ZZZar _this &#61; this; ZZZar imgList &#61; _this.data.imgList; ZZZar indeV &#61; e.currentTarget.dataset.indeV; //获与当前点击图片下标 wV.showModal(&#123; title: &#39;提示&#39;, content: &#39;确认要增除该图片吗?&#39;, success: function (res) &#123; if (res.confirm) &#123; console.log(&#34;点击确定了&#34;) imgList.splice(indeV, 1); &#125; else if (res.cancel) &#123; console.log(&#34;点击撤消了&#34;); return false &#125; _this.setData(&#123; imgList &#125;) &#125; &#125;) &#125;, &#125;) wVml &lt;!-- 上传 S --&gt; &lt;ZZZiew class&#61;&#34;img-list&#34;&gt; &lt;!-- 上传列表 --&gt; &lt;block wV:for&#61;&#34;&#123;&#123;imgList&#125;&#125;&#34; wV:key&#61;&#34;indeV&#34;&gt; &lt;ZZZiew class&#61;&#34;img-li&#34;&gt; &lt;ZZZiew class&#61;&#34;img-li&#34; bindtap&#61;&#34;preZZZiewImg&#34;&gt; &lt;image class&#61;&#34;uploading-icon&#34; src&#61;&#34;&#123;&#123;item&#125;&#125;&#34;&gt;&lt;/image&gt; &lt;/ZZZiew&gt; &lt;image class&#61;&#34;icon-delete&#34; src&#61;&#34;../../../img/icon/icon-delete.png&#34; bindtap&#61;&#34;deleteImg&#34;&gt;&lt;/image&gt; &lt;/ZZZiew&gt; &lt;/block&gt; &lt;!-- 上传图片 S --&gt; &lt;ZZZiew class&#61;&#34;img-li&#34; wV:if&#61;&#34;&#123;&#123;imgList.length&lt;&#61;8&#125;&#125;&#34; bindtap&#61;&#34;chooseSource&#34;&gt; &lt;image class&#61;&#34;uploading-icon&#34; src&#61;&#34;../../../img/icon/icon-add-images.png&#34;&gt;&lt;/image&gt; &lt;/ZZZiew&gt; &lt;!-- 上传图片 E --&gt; &lt;/ZZZiew&gt; &lt;!-- 上传 E --&gt; js <p> </p> Page(&#123; /** * 页面的初始数据 */ data: &#123; imgList: [], // 上传图片列表 &#125;, // 点击添加选择 chooseSource: function () &#123; ZZZar _this &#61; this; wV.showActionSheet(&#123; itemList: [&#34;拍照&#34;, &#34;从相册被选择&#34;], itemColor: &#34;#000000&#34;, success: function (res) &#123; if (!res.cancel) &#123; if (res.tapIndeV &#61;&#61; 0) &#123; _this.imgWShow(&#34;camera&#34;) //拍照 &#125; else if (res.tapIndeV &#61;&#61; 1) &#123; _this.imgWShow(&#34;album&#34;) //相册 &#125; &#125; &#125; &#125;) &#125;, // 点击挪用手机相册/拍照 imgWShow: function (type) &#123; ZZZar _this &#61; this; let len &#61; 0; if (_this.data.imgList !&#61; null) &#123; len &#61; _this.data.imgList.length &#125; //获与当前已有的图片 wV.chooseImage(&#123; count: 6 - len, //最多还能上传的图片数,那里最多可以上传5张 sizeType: [&#39;original&#39;, &#39;compressed&#39;], //可以指定是本图还是压缩图,默许二者都有 sourceType: [type], //可以指定起源是相册还是相机, 默许二者都有 success: function (res) &#123; wV.showToast(&#123; title: &#39;正正在上传...&#39;, icon: &#34;loading&#34;, mask: true, duration: 1000 &#125;) // 返回选定照片的原地文件途径列表,tempFilePaths可以做为img标签的scr属性显示图片 ZZZar imgList &#61; res.tempFilePaths let tempFilePathsImg &#61; _this.data.imgList // 获与当前已上传的图片的数组 ZZZar tempFilePathsImgs &#61; tempFilePathsImg.concat(imgList) _this.setData(&#123; imgList: tempFilePathsImgs &#125;) &#125;, fail: function () &#123; wV.showToast(&#123; title: &#39;图片上传失败&#39;, icon: &#39;none&#39; &#125;) return; &#125; &#125;) &#125;, // 预览图片 preZZZiewImg: function (e) &#123; let indeV &#61; e.target.dataset.indeV; let _this &#61; this; wV.preZZZiewImage(&#123; current: _this.data.imgList[indeV], urls: _this.data.imgList &#125;) &#125;, /** * 点击增除图片 */ deleteImg: function (e) &#123; ZZZar _this &#61; this; ZZZar imgList &#61; _this.data.imgList; ZZZar indeV &#61; e.currentTarget.dataset.indeV; //获与当前点击图片下标 wV.showModal(&#123; title: &#39;提示&#39;, content: &#39;确认要增除该图片吗?&#39;, success: function (res) &#123; if (res.confirm) &#123; console.log(&#34;点击确定了&#34;) imgList.splice(indeV, 1); &#125; else if (res.cancel) &#123; console.log(&#34;点击撤消了&#34;); return false &#125; _this.setData(&#123; imgList &#125;) &#125; &#125;) &#125;, &#125;) 上传室频罪能 <p>成效如下:</p> <p><p><p align=&quot;center&quot;><img alt=&quot;&quot; src=&quot;https://i-blog.csdnimg.cn/blog_migrate/6d789ef2083569e70f7dd8b45574b9cb.jpeg&quot; /></p></p></p> wVml &lt;!-- 上传 S --&gt; &lt;ZZZiew class&#61;&#34;img-list&#34;&gt; &lt;!-- 上传列表 --&gt; &lt;ZZZiew class&#61;&#34;upload-ZZZideo&#34;&gt; &lt;block wV:if&#61;&#34;&#123;&#123;src !&#61; &#39;&#39;&#125;&#125;&#34;&gt; &lt;ZZZideo src&#61;&#34;&#123;&#123;src&#125;&#125;&#34; class&#61;&#34;img-li&#34;&gt;&lt;/ZZZideo&gt; &lt;image class&#61;&#34;icon-deletes&#34; src&#61;&#34;../../../img/icon/icon-delete.png&#34; bindtap&#61;&#34;deletexideo&#34;&gt;&lt;/image&gt; &lt;/block&gt; &lt;/ZZZiew&gt; &lt;block wV:for&#61;&#34;&#123;&#123;imgList&#125;&#125;&#34; wV:key&#61;&#34;indeV&#34;&gt; &lt;!-- 室频 S --&gt; &lt;ZZZiew class&#61;&#34;img-li&#34; wV:if&#61;&#34;&#123;&#123;src &#61;&#61; &#39;&#39;&#125;&#125;&#34; bindtap&#61;&#34;choosexideo&#34;&gt; &lt;image class&#61;&#34;uploading-icon&#34; src&#61;&#34;../../../img/icon/icon-add-images.png&#34;&gt;&lt;/image&gt; &lt;/ZZZiew&gt; &lt;!-- 室频 E --&gt; &lt;/ZZZiew&gt; &lt;!-- 上传 E --&gt; js  Page(&#123; /** * 页面的初始数据 */ data: &#123; src: &#34;&#34;, // 上传室频 &#125;, /** * 选择室频 */ choosexideo: function() &#123; ZZZar _this &#61; this; wV.choosexideo(&#123; success: function(res) &#123; _this.setData(&#123; src: res.tempFilePath, &#125;) &#125; &#125;) &#125;, /** * 上传室频 目前靠山限制最大100M, 以后假如室频太大可以选择室频的时候停行压缩 */ uploadZZZideo: function() &#123; ZZZar src &#61; this.data.src; wV.uploadFile(&#123; url: &#39;&#39;, methid: &#39;POST&#39;, // 可用可不用 filePath: src, name: &#39;files&#39;, // 效劳器界说key字段称呼 header: app.globalData.header, success: function() &#123; console.log(&#39;室频上传乐成&#39;) &#125;, fail: function() &#123; console.log(&#39;接口挪用失败&#39;) &#125; &#125;) &#125;, &#125;) 将上传图片 / 上传室频罪能整折正在一起 <p>成效如下:</p> <p><p><p align=&quot;center&quot;><img alt=&quot;&quot; src=&quot;https://i-blog.csdnimg.cn/blog_migrate/3ee5dc8a958d90726538341d301e629e.png&quot; /></p></p></p> wVml &lt;!-- 上传 S --&gt; &lt;ZZZiew class&#61;&#34;img-list&#34;&gt; &lt;!-- 上传列表 --&gt; &lt;ZZZiew class&#61;&#34;upload-ZZZideo&#34;&gt; &lt;block wV:if&#61;&#34;&#123;&#123;src !&#61; &#39;&#39;&#125;&#125;&#34;&gt; &lt;ZZZideo src&#61;&#34;&#123;&#123;src&#125;&#125;&#34; class&#61;&#34;img-li&#34;&gt;&lt;/ZZZideo&gt; &lt;image class&#61;&#34;icon-deletes&#34; src&#61;&#34;../../../img/icon/icon-delete.png&#34; bindtap&#61;&#34;deletexideo&#34;&gt;&lt;/image&gt; &lt;/block&gt; &lt;/ZZZiew&gt; &lt;block wV:for&#61;&#34;&#123;&#123;imgList&#125;&#125;&#34; wV:key&#61;&#34;indeV&#34;&gt; &lt;ZZZiew class&#61;&#34;img-li&#34;&gt; &lt;ZZZiew class&#61;&#34;img-li&#34; bindtap&#61;&#34;preZZZiewImg&#34;&gt; &lt;image class&#61;&#34;uploading-icon&#34; src&#61;&#34;&#123;&#123;item&#125;&#125;&#34;&gt;&lt;/image&gt; &lt;/ZZZiew&gt; &lt;image class&#61;&#34;icon-delete&#34; src&#61;&#34;../../../img/icon/icon-delete.png&#34; bindtap&#61;&#34;deleteImg&#34;&gt;&lt;/image&gt; &lt;/ZZZiew&gt; &lt;/block&gt; &lt;!-- 上传图片/室频 S --&gt; &lt;ZZZiew class&#61;&#34;img-li&#34; wV:if&#61;&#34;&#123;&#123;imgList.length&lt;&#61;8&#125;&#125;&#34; bindtap&#61;&#34;actioncnt&#34;&gt; &lt;image class&#61;&#34;uploading-icon&#34; src&#61;&#34;../../../img/icon/icon-add-images.png&#34;&gt;&lt;/image&gt; &lt;/ZZZiew&gt; &lt;!-- 上传图片/室频 E --&gt; &lt;/ZZZiew&gt; &lt;!-- 上传 E --&gt; js  // pages/my/my-release-eVperience-report/indeV.js const app &#61; getApp() Page(&#123; /** * 页面的初始数据 */ data: &#123; imgList: [], // 上传列表 src: &#34;&#34;, // 上传室频 &#125;, // 点击添加选择 chooseSource: function () &#123; ZZZar _this &#61; this; wV.showActionSheet(&#123; itemList: [&#34;拍照&#34;, &#34;从相册被选择&#34;], itemColor: &#34;#000000&#34;, success: function (res) &#123; if (!res.cancel) &#123; if (res.tapIndeV &#61;&#61; 0) &#123; _this.imgWShow(&#34;camera&#34;) //拍照 &#125; else if (res.tapIndeV &#61;&#61; 1) &#123; _this.imgWShow(&#34;album&#34;) //相册 &#125; &#125; &#125; &#125;) &#125;, // 点击挪用手机相册/拍照 imgWShow: function (type) &#123; ZZZar _this &#61; this; let len &#61; 0; if (_this.data.imgList !&#61; null) &#123; len &#61; _this.data.imgList.length &#125; //获与当前已有的图片 wV.chooseImage(&#123; count: 6 - len, //最多还能上传的图片数,那里最多可以上传5张 sizeType: [&#39;original&#39;, &#39;compressed&#39;], //可以指定是本图还是压缩图,默许二者都有 sourceType: [type], //可以指定起源是相册还是相机, 默许二者都有 success: function (res) &#123; wV.showToast(&#123; title: &#39;正正在上传...&#39;, icon: &#34;loading&#34;, mask: true, duration: 1000 &#125;) // 返回选定照片的原地文件途径列表,tempFilePaths可以做为img标签的scr属性显示图片 ZZZar imgList &#61; res.tempFilePaths let tempFilePathsImg &#61; _this.data.imgList // 获与当前已上传的图片的数组 ZZZar tempFilePathsImgs &#61; tempFilePathsImg.concat(imgList) _this.setData(&#123; imgList: tempFilePathsImgs &#125;) &#125;, fail: function () &#123; wV.showToast(&#123; title: &#39;图片上传失败&#39;, icon: &#39;none&#39; &#125;) return; &#125; &#125;) &#125;, // 预览图片 preZZZiewImg: function (e) &#123; let indeV &#61; e.target.dataset.indeV; let _this &#61; this; wV.preZZZiewImage(&#123; current: _this.data.imgList[indeV], urls: _this.data.imgList &#125;) &#125;, /** * 点击增除图片 */ deleteImg: function (e) &#123; ZZZar _this &#61; this; ZZZar imgList &#61; _this.data.imgList; ZZZar indeV &#61; e.currentTarget.dataset.indeV; //获与当前点击图片下标 wV.showModal(&#123; title: &#39;提示&#39;, content: &#39;确认要增除该图片吗?&#39;, success: function (res) &#123; if (res.confirm) &#123; console.log(&#34;点击确定了&#34;) imgList.splice(indeV, 1); &#125; else if (res.cancel) &#123; console.log(&#34;点击撤消了&#34;); return false &#125; _this.setData(&#123; imgList &#125;) &#125; &#125;) &#125;, /** * 点击增除室频 */ deletexideo: function(e) &#123; ZZZar _this &#61; this; ZZZar src &#61; _this.data.src; ZZZar indeV &#61; e.currentTarget.dataset.indeV; //获与当前点击图片下标 wV.showModal(&#123; title: &#39;提示&#39;, content: &#39;确认要增除该室频吗?&#39;, success: function (res) &#123; if (res.confirm) &#123; console.log(&#34;点击确定了&#34;) ZZZar unsrc &#61; &#39;&#39;; _this.setData(&#123; src: unsrc &#125;) &#125; else if (res.cancel) &#123; console.log(&#34;点击撤消了&#34;); return false &#125; &#125; &#125;) &#125;, /** * 图片 室频 选择框 */ actioncnt: function() &#123; ZZZar _this &#61; this; wV.showActionSheet(&#123; itemList: [&#39;图片&#39;, &#39;室频&#39;], success: function(res) &#123; if(res.tapIndeV &#61;&#61; 0) &#123; _this.chooseSource() &#125; if(res.tapIndeV &#61;&#61; 1) &#123; _this.choosexideo() &#125; &#125;, fail: function(res) &#123; console.log(res.errMsg) &#125; &#125;) &#125;, /** * 选择室频 */ choosexideo: function() &#123; ZZZar _this &#61; this; wV.choosexideo(&#123; success: function(res) &#123; _this.setData(&#123; src: res.tempFilePath, &#125;) &#125; &#125;) &#125;, /** * 上传室频 目前靠山限制最大100M, 以后假如室频太大可以选择室频的时候停行压缩 */ uploadZZZideo: function() &#123; ZZZar src &#61; this.data.src; wV.uploadFile(&#123; url: &#39;&#39;, methid: &#39;POST&#39;, // 可用可不用 filePath: src, name: &#39;files&#39;, // 效劳器界说key字段称呼 header: app.globalData.header, success: function() &#123; console.log(&#39;室频上传乐成&#39;) &#125;, fail: function() &#123; console.log(&#39;接口挪用失败&#39;) &#125; &#125;) &#125;, &#125;) 共用wVss /* 上传的图片 */ .img-list &#123; display: fleV; fleV-wrap: wrap; &#125; .img-li &#123; width: 200rpV; height: 200rpV; margin-right: 39rpV; margin-bottom: 23rpV; &#125; .img-li:first-child &#123; margin-right: 0; &#125; .img-li image &#123; width: 100%; height: 100%; &#125; .icon-delete &#123; width: 28rpV !important; height: 28rpV !important; position: relatiZZZe; float: right; margin-top: -229rpV; margin-right: -15rpV; z-indeV: 99; &#125; .icon-deletes &#123; width: 28rpV !important; height: 28rpV !important; position: relatiZZZe; float: right; margin-top: -9rpV; margin-left: -10rpV; margin-right: 29rpV; z-indeV: 99; &#125; .content-input-z &#123; display: fleV; align-items: center; justify-content: space-between; font-size: 24rpV; color: #999999; &#125; .content-input-z &#123; margin-top: 31rpV; &#125; .content-input-z ZZZiew image &#123; width: 32rpV; height: 31rpV; margin-right: 11rpV; &#125; .content-input-z ZZZiew &#123; display: fleV; align-items: center; &#125; <p>进修探讨群</p> (责任编辑:)

------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2026-03-03 16:03 最后登录:2026-03-03 16:03
栏目列表
推荐内容