• uniapp上传二进制图片


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

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

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

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

    在page底下的vue文件引入

    1. @import "../../colorui/main.css";
    2. @import "../../colorui/icon.css";

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

    1. <template>
    2. <view>
    3. <form>
    4. <view class="cu-bar bg-white margin-top">
    5. <view class="action">
    6. 图片上传
    7. </view>
    8. <view class="action">
    9. {{imgList.length}}/4
    10. </view>
    11. </view>
    12. <view class="cu-form-group">
    13. <view class="grid col-4 grid-square flex-sub">
    14. <view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
    15. <image :src="imgList[index]" mode="aspectFill"></image>
    16. <view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
    17. <text class='cuIcon-close'></text>
    18. </view>
    19. </view>
    20. <view class="solids" @tap="ChooseImage" v-if="imgList.length<4">
    21. <text class='cuIcon-cameraadd'></text>
    22. </view>
    23. </view>
    24. </view>
    25. </form>
    26. </view>
    27. </template>
    28. <script>
    29. export default {
    30. data() {
    31. return {
    32. imgList: [],
    33. // modalName: null,
    34. };
    35. },
    36. methods: {
    37. ChooseImage() {
    38. uni.chooseImage({
    39. count: 4, //默认9
    40. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
    41. sourceType: ['album'], //从相册选择
    42. success: (res) => {
    43. if (this.imgList.length != 0) {
    44. this.imgList = this.imgList.concat(res.tempFilePaths)
    45. } else {
    46. this.imgList = res.tempFilePaths
    47. }
    48. }
    49. });
    50. },
    51. ViewImage(e) {
    52. uni.previewImage({
    53. urls: this.imgList,
    54. current: e.currentTarget.dataset.url
    55. });
    56. },
    57. //删除
    58. DelImg(e) {
    59. uni.showModal({
    60. title: '删除',
    61. content: '确定要删除这张图片?',
    62. cancelText: '取消',
    63. confirmText: '删除',
    64. success: res => {
    65. if (res.confirm) {
    66. this.imgList.splice(e.currentTarget.dataset.index, 1)
    67. }
    68. }
    69. })
    70. },
    71. }
    72. }
    73. </script>
    74. <style>
    75. @import "../../colorui/main.css";
    76. @import "../../colorui/icon.css";
    77. .cu-form-group .title {
    78. min-width: calc(4em + 15px);
    79. }
    80. </style>

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

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

    1. success: (res) => {
    2. const tempFilePaths = res.tempFilePaths;
    3. const uploadTask = uni.uploadFile({
    4. url: 'http://47.xxx.xxx.78:8088/chemApp/work/upload.action',
    5. filePath: tempFilePaths[0],
    6. name: 'file',
    7. success: function(uploadFileRes) {
    8. console.log(uploadFileRes);
    9. _this.imgList = [..._this.imgList, uploadFileRes.data]
    10. }
    11. });
    12. }

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

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

    1. <template>
    2. <view>
    3. <form>
    4. <view class="cu-bar bg-white margin-top">
    5. <view class="action">
    6. 图片上传
    7. </view>
    8. <view class="action">
    9. {{imgList.length}}/4
    10. </view>
    11. </view>
    12. <view class="cu-form-group">
    13. <view class="grid col-4 grid-square flex-sub">
    14. <view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="item">
    15. <image v-if="item" :src="item" mode="aspectFill"></image>
    16. <view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
    17. <text class='cuIcon-close'></text>
    18. </view>
    19. </view>
    20. <view class="solids" @tap="ChooseImage" v-if="imgList.length<4">
    21. <text class='cuIcon-cameraadd'></text>
    22. </view>
    23. </view>
    24. </view>
    25. </form>
    26. </view>
    27. </template>
    28. <script>
    29. export default {
    30. data() {
    31. return {
    32. imgList: [],
    33. // modalName: null,
    34. };
    35. },
    36. methods: {
    37. ChooseImage() {
    38. const _this = this
    39. uni.chooseImage({
    40. count: 4, //默认9
    41. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
    42. sourceType: ['album'], //从相册选择
    43. success: (res) => {
    44. const tempFilePaths = res.tempFilePaths;
    45. const uploadTask = uni.uploadFile({
    46. url : 'http://47.xxx.xxx.78:8088/chemApp/work/upload.action',
    47. filePath: tempFilePaths[0],
    48. name: 'file',
    49. success: function (uploadFileRes) {
    50. console.log(uploadFileRes);
    51. _this.imgList = [..._this.imgList, uploadFileRes.data]
    52. }
    53. });
    54. }
    55. });
    56. },
    57. ViewImage(e) {
    58. uni.previewImage({
    59. urls: this.imgList,
    60. current: e.currentTarget.dataset.url
    61. });
    62. },
    63. //删除
    64. DelImg(e) {
    65. uni.showModal({
    66. title: '删除',
    67. content: '确定要删除这张图片?',
    68. cancelText: '取消',
    69. confirmText: '删除',
    70. success: res => {
    71. if (res.confirm) {
    72. this.imgList.splice(e.currentTarget.dataset.index, 1)
    73. }
    74. }
    75. })
    76. },
    77. }
    78. }
    79. </script>
    80. <style>
    81. @import "../../colorui/main.css";
    82. @import "../../colorui/icon.css";
    83. .cu-form-group .title {
    84. min-width: calc(4em + 15px);
    85. }
    86. </style>

    上传图片效果

  • 相关阅读:
    TIA博途_通过PEEK指令在TP900触摸屏上实现监控所有IO地址的具体方法示例
    Node学习七 —— 创建和控制外部进程
    LeetCode 每日一题 2022/10/31-2022/11/6
    不知不觉做测试也两年了,该学点什么才能更有发展前景~
    执行sql报错only_full_group_by的解决方法
    软件测试银行项目网上支付接口调用测试实例
    PostgreSQL17.0中IS NOT NULL和IS NULL查询限制优化功能测试
    分库分表的 21 条法则,hold 住!
    【保姆级教程】Windows 远程登陆 Linux 服务器的两种方式:SSH + VS Code,开发必备
    Go进阶之rpc和grpc
  • 原文地址:https://blog.csdn.net/qq_36538012/article/details/125456942