首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >uniapp上传图片至服务器,获得在线图片链接预览(实战)

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

作者头像
王小婷
发布于 2020-08-11 07:36:15
发布于 2020-08-11 07:36:15
10.8K20
代码可运行
举报
文章被收录于专栏:编程微刊编程微刊
运行总次数: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 删除。

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

评论
登录后参与评论
2 条评论
热度
最新
麻烦问下您,为什么图片上传不能显示啊
麻烦问下您,为什么图片上传不能显示啊
11点赞举报
我也出现了相同的问题 您那找到解决的方案了吗
我也出现了相同的问题 您那找到解决的方案了吗
回复回复点赞举报
推荐阅读
编辑精选文章
换一批
vue之图片上传组件封装
代码已上传至github github代码地址:https://github.com/Miofly/mio.git
用户10106350
2022/10/28
5910
微信小程序实现上传图片功能[通俗易懂]
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/150595.html原文链接:https://javaforall.cn
全栈程序员站长
2022/06/28
5.2K0
微信小程序实现上传图片功能[通俗易懂]
小程序使用 Promise.all 完成文件异步上传
extends [微信小程序开发技巧总结(二) -- 文件的选取、移动、上传和下载 - Kindear - 博客园 (cnblogs.com)]
Kindear
2021/04/26
1.7K0
5.页面绘制-主题列表页(使用ColorUI、uni-app官方组件)
在pages/shequ目录下新建页面zhuti_list,然后在pages.json将zhuti_list配置为首页,方便查看。
玩蛇的胖纸
2021/07/08
3.4K0
uni-app 结合云函数开发小程序博客(二):云函数实现登录注册
不好意思大家,个人原因拖了一周时间才发表第二篇,没想到还有朋友在支持,非常感谢和抱歉。第一篇中已经引入了第三方样式,实现了主题和语言的切换;本篇主要开始页面的搭建和云函数创建,前端代码通过uniCloud.callFunction()方法调用云函数,云函数中可执行js运算、读写云数据库(NoSQL),直接返回json数据,也可以使用npm安装第三方依赖。服务端环境安装配置和安全等方面完全不需要去考虑。
一只图雀
2020/05/07
3.5K0
超有意思,圣诞节自己做一个装饰圣诞帽头像的APP!
话说又到了一年一度到别人到节日,圣诞节,还记得去年的时候,朋友圈疯狂转发到圣诞帽嘛,在圣诞节为自己到头像增加一款圣诞帽还是蛮应景的。
程序员小二
2021/12/25
3810
超有意思,圣诞节自己做一个装饰圣诞帽头像的APP!
17.普通用户、网格长、网格员,操作数据(4)newapp/components/wgz.vue
1.在newapp/components/wgz.vue中: <template> <view> <view class="cu-bar bg-white solid-bottom"> <view class="action"> <text class="cuIcon-title text-orange"></text> {{data3.pq.name}} </view>
玩蛇的胖纸
2020/07/03
3950
uni-app 图片上传实战
uni.uploadFile() 将本地资源上传到开发者服务器 客户端发起一个post请求 content-type
达达前端
2019/10/14
5.7K0
11.开发newapp个人中心pages/me/me.vue和修改密码功能
1.在小程序端newapp开发个人中心页面: 1.备用 1.新建修改密码页面uppwd  2.开发pages/me/me.vue: <template> <view class="conten
玩蛇的胖纸
2020/06/22
5430
18.普通用户、网格长、网格员,操作数据(5)newapp/components/wgy.vue
1.在newapp/components/wgy.vue中: <template> <view> <scroll-view scroll-x class="bg-cyan na
玩蛇的胖纸
2020/07/03
6120
小程序上传图片加水印
注意事项: 1.如果在画图完成后,有对应操作时,必须在draw的callback后执行,比如图片预览、改变画布大小等 ctx.draw(false, function() { console.log(‘后续操作’);
全栈程序员站长
2022/07/04
1.2K0
uni-app插件ColorUI步骤条
1. uni-app插件ColorUI步骤条 1.1. 前言 uni-app就不介绍了,前面几篇已经有所介绍,不知道的可以翻看我前面几篇博客 ColorUI-uniApp是uni-app的一款ui组件
老梁
2019/09/10
5.3K0
uni-app插件ColorUI步骤条
微信小程序上传图片
//添加图片 joinPicture: function (e) { var index = e.currentTarget.dataset.index; var evalList = this.data.evalList; var that = this; var imgNumber = evalList[index].tempFilePaths; if (imgNumber.length >= 3) { wx.showModal({
达达前端
2022/04/29
1.8K0
uni-app实图片和视频上传
使用uni-app实现点击上传,既可以上传视频,有可以上传图片,图片预览,删除图片和视频功能,最终效果如下。uni-app里面没有提供同时上传视频和图片这个插件,只能靠自己手写,
小周sir
2019/10/21
7.7K0
微信小程序----图片预览
效果图 原理 使用wx.chooseImage选择本地图片; 使用wx.previewImage预览图片。 WXML <view> <button bindtap="previewImage" t
Rattenking
2021/02/01
2K0
微信小程序----图片预览
uni-app实战教程-----H5移动app以及小程序(五)---再次开发前端
将index.vue中的goTest() 复制到 apiuse中并改成delImg 如下
代码哈士奇
2021/02/04
7910
uni-app实战教程-----H5移动app以及小程序(五)---再次开发前端
借助云开发实现小程序朋友圈的发布与展示丨实战
这里就不多说了,注意:一定要用自己的appid,所以你需要注册一个小程序(个人的就行)
腾讯云开发TCB
2019/10/22
9460
借助云开发实现小程序朋友圈的发布与展示
这里就不多说了,如果你还不知道如何创建小程序项目可以去翻看下我之前的文章,也可以看下我录制的《10小时零基础入门小程序开发》
编程小石头
2019/10/12
8490
借助云开发实现小程序朋友圈的发布与展示
图片上传区 -组件封装
1.效果 2.代码 <template> <view> <view class="uni-common-mt"> <view class="uni-uploader" v-if="imageListAreaShow"> <view class="uni-uploader-head"> <view class="uni-uploader-title">点击可预览选好的图片</vi
名字是乱打的
2022/11/06
6230
图片上传区  -组件封装
12.开发newapp修改Bug1:用户信息应该实时更新,网格长数据展示,网格长网格员编辑数据的权力的查询
1.修改Bug1:用户信息应该实时更新 1.修改潜在bug,因为后端可能要涉及到用户身份权限的修改,所以每一次打开个人中心和操作中心,都应该更新一遍用户的个人信息 1.在后端项目user_operations/views.py中: class UpUserInfoView(APIView): """更新用户信息""" def get(self, request): token = request.GET.get('token') if token:
玩蛇的胖纸
2020/06/28
9780
推荐阅读
相关推荐
vue之图片上传组件封装
更多 >
交个朋友
加入前端学习入门群
前端基础系统教学 经验分享避坑指南
加入腾讯云技术交流站
前端技术前沿探索 云开发实战案例分享
加入云开发企业交流群
企业云开发实战交流 探讨技术架构优化
换一批
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档