肥锦 發表於 2020-4-27 16:04:00

uni-app 上传图片之压缩图片上传

<p>原文:https://ask.dcloud.net.cn/article/36331?notification_id-701034__item_id-28962</p>
<p>应后台要求,上传得图片不得大于500kb,但我发现拍照或者相册里面的照片普遍大于1M,试用多种方法之后,终于在移动端测试成功,以下是全部代码</p>
<p><strong>clickImg()</strong>页面点击事件,触发选择图片/拍照事件<br>
<strong>uploadFile()</strong> 上传图片,使用async同步化,压缩完再进行上传。<br>
<strong>compress()</strong> 压缩图片</p>
<ul>
<li>5+方法限于移动端使用,因此要限制一下平台,很重要!!!</li>
<li>async await--上传图片之前先压缩,否则上传会比压缩先执行</li>
</ul>
<p>压缩图片api方法:https://www.html5plus.org/doc/zh_cn/zip.html<br>
效果:<br>
<img src="https://img2020.cnblogs.com/blog/2014339/202011/2014339-20201102175800330-754584119.png"></p>
<pre class="brush:js;">//html部分
      <template>
            <view class="page">
                  <view>uni-app本地图片压缩+上传</view>
                  <view class="photo-box ">
                        <button type="primary" @tap="clickImg()">点击上传</button>
                        <view v-if="image">
                        <img src="https://img2020.cnblogs.com/blog/2014339/202011/2014339-20201102173259170-422155575.png"></p><br><br>
来源:https://www.cnblogs.com/Lucyy/p/12787381.html
頁: [1]
查看完整版本: uni-app 上传图片之压缩图片上传