Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >uniapp上传图片至服务器,获得在线图片链接预览(实战)

uniapp上传图片至服务器,获得在线图片链接预览(实战)

作者头像
王小婷
发布于 2020-08-11 07:36:15
发布于 2020-08-11 07:36:15
10.7K00
代码可运行
举报
文章被收录于专栏:编程微刊编程微刊
运行总次数:0
代码可运行

功能需求: 前端选择本地文件,将选择好的文件显示在界面上进行预览,可同时选择四张进行预览。

思路如下: 前端选择本地的png、jpg、等格式的图片,将图片以二进制的形式传到后端服务器,后端对二进制图片进行处理,返回给前端一个服务器链接在线图片,在浏览器就可以打开链接访问的那种。然后前端将这个图片链接渲染在页面进行预览。

首先 我们看一下uniapp的官方文档: https://uniapp.dcloud.io/api/media/image?id=chooseimage

大概是这样的 先写一个模拟的demo 1:首先我是是用了colorUI的框架,在项目里面引入

在page底下的vue文件引入

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@import "../../colorui/main.css";
@import "../../colorui/icon.css";

这样一来,就不需要写什么样式了,直接使用写好的就行了。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<template>
    <view>
        <form>
            <view class="cu-bar bg-white margin-top">
                <view class="action">
                    图片上传
                </view>
                <view class="action">
                    {{imgList.length}}/4
                </view>
            </view>
            <view class="cu-form-group">
                <view class="grid col-4 grid-square flex-sub">
                    <view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
                     <image :src="imgList[index]" mode="aspectFill"></image>
                        <view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
                            <text class='cuIcon-close'></text>
                        </view>
                    </view>
                    <view class="solids" @tap="ChooseImage" v-if="imgList.length<4">
                        <text class='cuIcon-cameraadd'></text>
                    </view>
                </view>
            </view>
            
        </form>
    </view>
</template>
<script>
    export default {
        data() {
            return {
                imgList: [],
            //  modalName: null,
            };
        },
        methods: {
        
            ChooseImage() {
                uni.chooseImage({
                    count: 4, //默认9
                    sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
                    sourceType: ['album'], //从相册选择
                    success: (res) => {
                        if (this.imgList.length != 0) {
                            this.imgList = this.imgList.concat(res.tempFilePaths)
                        } else {
                            this.imgList = res.tempFilePaths
                        }
                    }
                });
            },
            ViewImage(e) {
                uni.previewImage({
                    urls: this.imgList,
                    current: e.currentTarget.dataset.url
                });
            },
            //删除
            DelImg(e) {
                uni.showModal({
                    title: '删除',
                    content: '确定要删除这张图片?',
                    cancelText: '取消',
                    confirmText: '删除',
                    success: res => {
                        if (res.confirm) {
                            this.imgList.splice(e.currentTarget.dataset.index, 1)
                        }
                    }
                })
            },
        }
    }
</script>

<style>
@import "../../colorui/main.css";
@import "../../colorui/icon.css";
.cu-form-group .title {
   min-width: calc(4em + 15px);
}
</style>

效果是这样的 每次选完图片之后显示在页面上,我这里设置了最多可以选择四张,图片链接使用了临时的blob,接下来就要使用后端小伙伴给的接口,将自己本地的二进制文件传给他了。

chooseImage选择好图片之后,写一个成功的回调函数,在回到函数里面添加一个图片上传的方法uploadFile,在方法里面添加url,等参数。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
success: (res) => {
                        const tempFilePaths = res.tempFilePaths;
                        const uploadTask = uni.uploadFile({
                            url: 'http://47.xxx.xxx.78:8088/chemApp/work/upload.action',
                            filePath: tempFilePaths[0],
                            name: 'file',
                            success: function(uploadFileRes) {
                                console.log(uploadFileRes);
                                _this.imgList = [..._this.imgList, uploadFileRes.data]

                            }
                        });
                    }

若是请求成功 则返回一个图片链接

添加接口之后 的,demo如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<template>
    <view>
        <form>
            <view class="cu-bar bg-white margin-top">
                <view class="action">
                    图片上传
                </view>
                <view class="action">
                    {{imgList.length}}/4
                </view>
            </view>
            <view class="cu-form-group">
                <view class="grid col-4 grid-square flex-sub">
                    <view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="item">
                     <image v-if="item" :src="item" mode="aspectFill"></image>
                        <view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
                            <text class='cuIcon-close'></text>
                        </view>
                    </view>
                    <view class="solids" @tap="ChooseImage" v-if="imgList.length<4">
                        <text class='cuIcon-cameraadd'></text>
                    </view>
                </view>
            </view>
            
        </form>
    </view>
</template>
<script>
    export default {
        data() {
            return {
                imgList: [],
            //  modalName: null,
            };
        },
        methods: {
        
            ChooseImage() {
                const _this = this
                uni.chooseImage({
                    count: 4, //默认9
                    sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
                    sourceType: ['album'], //从相册选择
                    success: (res) => {
                         const tempFilePaths = res.tempFilePaths;
                             const uploadTask = uni.uploadFile({
                              url : 'http://47.xxx.xxx.78:8088/chemApp/work/upload.action',
                              filePath: tempFilePaths[0],
                              name: 'file',
                             
                              success: function (uploadFileRes) {
                               console.log(uploadFileRes);
                               _this.imgList = [..._this.imgList, uploadFileRes.data]
                               
                              }
                             });
                       
                    }
                });
            },
            ViewImage(e) {
                uni.previewImage({
                    urls: this.imgList,
                    current: e.currentTarget.dataset.url
                });
            },
            //删除
            DelImg(e) {
                uni.showModal({
                    title: '删除',
                    content: '确定要删除这张图片?',
                    cancelText: '取消',
                    confirmText: '删除',
                    success: res => {
                        if (res.confirm) {
                            this.imgList.splice(e.currentTarget.dataset.index, 1)
                        }
                    }
                })
            },
        }
    }
</script>

<style>
@import "../../colorui/main.css";
@import "../../colorui/icon.css";
.cu-form-group .title {
   min-width: calc(4em + 15px);
}
</style>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
写给 Android 开发者的 I/O 18 大会精华导读
Google 发布了 Android Jetpack,帮助开发者加快应用开发速度。
HyperAI超神经
2019/12/02
9220
聚焦 Android 11: 大功告成
这是 #11WeeksOfAndroid 系列的最后一篇文章。感谢您在过去的时间里和我们一起深入探索 Android 开发的关键领域。下面来和我们一起回顾这些精彩内容吧:
Android 开发者
2022/09/23
2.4K0
聚焦 Android 11: Jetpack
在往期 #11WeeksOfAndroid 系列文章中我们介绍了 联系人和身份 、隐私和安全 、 Android 11 兼容性 、 开发语言 ,本期将聚焦  Jetpack 。我们将为大家陆续带来 #11WeeksOfAndroid 内容,深入探讨 Android 的各个关键技术点,您不会错过任何重要内容。
Android 开发者
2020/10/16
9700
Android 开发者峰会 2019 中有哪些不容错过的重点?
希望开发者们以及家人都平平安安,健健康康!也许有的开发者还暂时没法回到办公室工作,也许有的开发者的发布日程会因为这次疫情而受到影响,但请相信,在每一个人的努力下,疫情会尽早过去,我们熟悉的那个春天会重回每一个人的身边。
Android 开发者
2020/02/25
7200
聚焦 Android 11 : 隐私和安全
上期 #11WeeksOfAndroid 系列文章中内容我们介绍了 联系人和身份,本期我们将聚焦 隐私和安全 。我们将为大家陆续带来 #11WeeksOfAndroid 内容,深入探讨 Android 的各个关键技术点,您不会错过任何重要内容。
Android 开发者
2020/10/16
1.5K0
聚焦 Android 11 : 隐私和安全
和 Google Play 一起展望未来
周一 (美国时间 8 月 6 日) 我们发布了 Android 9 Pie。在持续推动 Android 平台发展的同时,我们也一直在寻求新的方法,帮助您提高应用的分发效率,让更多用户发现和喜爱上您的作品,并提升我们生态系统的整体安全性。Google Play 今年取得了一系列重要的里程碑,助力开发者获得更多用户:
Android 开发者
2018/08/28
1.1K0
和 Google Play 一起展望未来
游戏优化利器 | Android GPU Inspector 开放 Beta 测试版
随着 Android 11 在 Pixel 上的推出,Android GPU Inspector (AGI) 得以从不公开测试的开发者预览版升级到开放的 Beta 测试版。在开发者预览版期间,AGI 帮助与我们合作的开发者们顺利发现了产品中的性能瓶颈。随着开放 Beta 版的到来,我们也期待听到大家的反馈。
Android 开发者
2020/10/16
1.7K0
游戏优化利器 | Android GPU Inspector 开放 Beta 测试版
对接google play支付
设置开发者帐号后,您必须发布包含 Google Play 结算库的应用版本。如需在 Google Play 管理中心启用结算相关功能(如配置您要销售的商品),必须执行此步骤。
崔哥
2022/09/08
2.5K0
GitHub Android 开源项目汇总
GitHub 上的开源项目不胜枚举,越来越多的开源项目正在迁移到GitHub平台上。基于不要重复造轮子的原则,了解当下比较流行的Android与iOS开源项目很是必要。利用这些项目,有时能够让你达到事半功倍的效果。
阳光岛主
2019/02/19
2.3K0
GitHub Android 开源项目汇总
App上线Google Play的流程
在海外上线 Android App 到 Google Play 商店的流程与在国内基本一致,但需要注意一些针对海外市场的特定事项。以下是详细的步骤。
数字孪生开发者
2025/04/11
3830
App上线Google Play的流程
为 Google Play 新增的安全部分做好准备
作者 / Suzanne Frey, VP, Product, Android Security and Privacy
Android 开发者
2022/03/09
3480
为 Google Play 新增的安全部分做好准备
Google Play In-app Billing
应用程序内部付费机制(Google Play In-app Billing, 以下简称应用内支付)是Google Play的一项服务,这种服务为应用内购买提供支付流程。
阳光岛主
2019/02/19
4.4K0
Google Play In-app Billing
Google Play 更新一览 | 2021 Android 开发者峰会
在今年的 Android 开发者峰会 上,我们分享了一些一直在构建的新功能,为您在我们平台上的发展提供助力,包括信任和安全方面的提升、提高您应用质量并改善获利的工具,还有一些针对游戏的更新,以及激动人心的全新应用营销证书。
Android 开发者
2022/03/24
8500
[译] Google Play 控制台指南:Google Play 控制台能为你做的都不仅仅是发布应用这么简单而已
原文地址:A guide to the Google Play Console 原文作者:Dom Elliott 译文出自:掘金翻译计划 本文永久链接:github.com/xitu/gold-m… 译者:JayZhaoBoy 校对者:hanliuxin5,IllllllIIl 无论你是企业用户还是作为技术人员,在 1 或 100 人的团队中,Play 控制台能为你做的都不仅仅是发布应用这么简单而已 你或许使用 Google Play 控制台上传过 Android 应用或者游戏,创建一个商品详情并点击上
Android 开发者
2018/05/31
8.2K0
Android 11 中的存储机制更新
Android 10 引入了对外部存储权限的更改,旨在更好地保护用户数据以及降低应用的存储空间。Android 11 开发者预览版里加入了更多改进,以帮助开发者更好地适应这些权限修改。
Android 开发者
2020/03/19
3.3K0
Google Play 政策更新提醒与重点解读 | 2021 年 4 月
本文介绍了 Google Play 开发者政策近期的一些重要更新,您也可以通过线上培训营视频进行回顾。
Android 开发者
2022/03/09
1.1K0
Google Play 政策更新提醒与重点解读 | 2021 年 4 月
Notes from Google Play | 蜕变之年
作者 / Google Play 合作伙伴关系部门副总裁 Purnima Kochikar
Android 开发者
2022/03/25
5140
Google Play 政策更新提醒与重点解读 | 2021 年第四季度
本文快速介绍以及回顾了 Google Play 开发者政策近期的重要更新,并深入解读了家庭政策方面的内容以帮助开发者们打造适合家庭和儿童的应用,您也可以通过线上 培训营视频 进行回顾。
Android 开发者
2022/03/24
1.1K0
Google Play 政策更新提醒与重点解读 | 2021 年第四季度
Android 游戏开发速递
作者 / Greg Hartrell, Head of Product Management, Games on Android & Google Play
Android 开发者
2020/06/16
1.3K0
Android 11 Beta 版正式发布!以及众多面向开发者的重磅更新
今天,我们发布 Android 11 Beta 版,并为开发者们带来了众多重磅更新。这些更新包括 Kotlin 协程、Jetpack Compose 工具包的最新进展、在 Android Studio 中更快地完成构建,以及 Play Console 的全新改版。
Android 开发者
2020/06/16
1.9K0
Android 11 Beta 版正式发布!以及众多面向开发者的重磅更新
推荐阅读
相关推荐
写给 Android 开发者的 I/O 18 大会精华导读
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验