爱吃香菜的啵啵 發表於 2020-9-17 15:00:00

uni-app实现图片上传,添加展示与删除

<p><span style="font-size: 18pt">如图,uni-app实现图片上传,添加展示与删除</span></p>
<p><img src="https://img2020.cnblogs.com/blog/1572968/202009/1572968-20200917144542909-119524048.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><img src="https://img2020.cnblogs.com/blog/1572968/202009/1572968-20200917145001376-1874443087.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 上传照片 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="item-num"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="upload-img"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                  <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 预览缩略图 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
                  <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="q-image-wrap"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 图片缩略图</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
                        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">block </span><span style="color: rgba(255, 0, 0, 1)">v-for</span><span style="color: rgba(0, 0, 255, 1)">="(imgItem, idx) in photoFiles"</span><span style="color: rgba(255, 0, 0, 1)"> :key</span><span style="color: rgba(0, 0, 255, 1)">="idx"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="item"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                              <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">image </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="q-image"</span><span style="color: rgba(255, 0, 0, 1)"> :src</span><span style="color: rgba(0, 0, 255, 1)">="imgItem.url"</span><span style="color: rgba(255, 0, 0, 1)"> mode</span><span style="color: rgba(0, 0, 255, 1)">="aspectFill"</span><span style="color: rgba(255, 0, 0, 1)"> :data-cur</span><span style="color: rgba(0, 0, 255, 1)">="imgItem.url"</span><span style="color: rgba(255, 0, 0, 1)"> @tap</span><span style="color: rgba(0, 0, 255, 1)">="refundPicPreView"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">image</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                              <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">image</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                              <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 移除图片的按钮</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
                              <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="q-image-remover"</span><span style="color: rgba(255, 0, 0, 1)"> :data-idx</span><span style="color: rgba(0, 0, 255, 1)">="idx"</span><span style="color: rgba(255, 0, 0, 1)"> @tap</span><span style="color: rgba(0, 0, 255, 1)">="removeImage"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                                    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> &lt;text&gt;x&lt;/text&gt; </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
                                    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">image </span><span style="color: rgba(255, 0, 0, 1)">src</span><span style="color: rgba(0, 0, 255, 1)">="../../static/icons/delect.png"</span><span style="color: rgba(255, 0, 0, 1)"> mode</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">image</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                              <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                            <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                        <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">block</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 添加图片图标 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
                        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">view </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="item"</span><span style="color: rgba(255, 0, 0, 1)"> v-if</span><span style="color: rgba(0, 0, 255, 1)">="photoFiles.length &lt; 6"</span><span style="color: rgba(255, 0, 0, 1)"> @tap</span><span style="color: rgba(0, 0, 255, 1)">="getUploadImg"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">image </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="q-image"</span><span style="color: rgba(255, 0, 0, 1)"> src</span><span style="color: rgba(0, 0, 255, 1)">="../../static/icons/default-img.png"</span><span style="color: rgba(255, 0, 0, 1)"> mode</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                            <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">image</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                        <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                  <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">view</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> end </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span></pre>
</div>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">静态的data数据:<br>data() {
            return {
                photoFiles: [
                  {url: "../../static/text.jpg",id:1},
                  {url: "../../static/text.jpg",id:2},
                  {url: "../../static/text.jpg",id:3},
                  {url: "../../static/text.jpg",id:4},
                  {url: "../../static/text.jpg",id:5}
                ], //凭证图片列表
            };
      },</span></pre>
</div>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">/**
             * 上传图片
             */
            getUploadImg: function(e) {
                var idx = e.target.dataset.idx;
                console.log(idx);
                var ths = this;
                wx.chooseImage({
                  count: 1,
                  // 默认9
                  sizeType: ['compressed'],
                  sourceType: ['album', 'camera'],
                  success: function(res) {
                        // 图片的本地临时文件路径列表
                        var tempFilePaths = res.tempFilePaths;
                        uni.showLoading({
                            mask: true
                        });
                        var params = {
                        url: "/p/file/upload",
                        filePath: tempFilePaths,
                        name: 'file',
                        callBack: function (res2) {
                            uni.hideLoading()
                            var img = {};
                            img.path = JSON.parse(res2).filePath;
                            img.url = JSON.parse(res2).resourcesUrl + JSON.parse(res2).filePath;
                            var photoFiles = ths.photoFiles;
                            photoFiles.push(img);
                            ths.setData({
                              photoFiles: photoFiles
                            });
                        }
                        };
                        http.upload(params);
                  }
                });
            },
            /**
             * 图片点击预览
             */
            refundPicPreView(e) {
                var current = e.currentTarget.dataset.cur
                var urls = []
                this.photoFiles.forEach(el =&gt; {
                  urls.push(el.url)
                })
                uni.previewImage({
                  current: current,
                  urls: urls
                })
            },

            /**
             * 删除图片
             */
            removeImage: function(e){
                var idx = e.currentTarget.dataset.idx;
                var photoFiles = this.photoFiles;
                photoFiles.splice(idx, 1);
                this.setData({
                  photoFiles: photoFiles
                })
            }</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">.item-num {}

.item-num .upload-img {
    padding: 40rpx 40rpx 40rpx 30rpx;
}

.item-num .upload-img .q-image-wrap {
    display: flex;
    /* height: 500rpx; */
    flex-wrap: wrap;
}

.item-num .upload-img .q-image-wrap .item {
    position: relative;
    height: 200rpx;
    width: 200rpx;
    margin-right: 20rpx;
    margin-bottom: 20rpx;
}

.item-num .upload-img .q-image-wrap .item .q-image {
    height: 200rpx;
    width: 200rpx;
}

.item-num .upload-img .q-image-wrap .item .q-image-remover {
    width: 0;
    height: 0;
    border-top: 66rpx solid #bfde85;
    border-left: 66rpx solid transparent;
    position: absolute;
    top: 0;
    right: 0;
}

/* .item-num .upload-img .q-image-wrap .item .q-image-remover text{
    width: 30rpx;
    display: block;
    height: 30rpx;
    color: #FFFFFF;
    text-align: center;
    line-height: 30rpx;
    font-size: 30rpx;
    border-radius: 20rpx;
    background-color: #bfde85;
    position: absolute;
    top: -60rpx;
    right: 0;
} */

.item-num .upload-img .q-image-wrap .item .q-image-remover image {
    width: 24rpx;
    height: 24rpx;
    position: absolute;
    top: -60rpx;
    right: 4rpx;
}</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span style="font-size: 18pt">上传的没搞明白,有待改进</span></p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/yoona-lin/p/13685230.html
頁: [1]
查看完整版本: uni-app实现图片上传,添加展示与删除