需要准备的工作:

①、建立微信小程序工程,编写以下代码。

②、通过IDE建立springboot+web工程,编写接收文件以及提供下载文件的方式,并将上传的文件相关信息记录在mysql数据库中。具体请查看https://www.cnblogs.com/chenfeifen/p/10261980.html

一、配置index.wxml

 <!--index.wxml-->
<view class="container">
<view class="userinfo">
<button bindtap="upload"> 上传原图</button>
<button bindtap="download"> 下载图片</button>
</view>
<view class="imginfo">
<block wx:for="{{tempFilePaths}}" wx:key="{{index}}">
<image src="{{item }}" bindtap="listenerButtonPreviewImage" data-index="{{index}}" style="width: 100%;"/>
</block>
<block> <image src='{{downloadPicturePath}}' bindtap='preview_download_picture'></image>
</block>
</view>
</view>

二、配置index.wxss

  1 /**index.wxss**/
2 .userinfo {
3 display: flex;
4 /* flex-direction: column; */
5 align-items: center;
6 }
7 .imginfo {
8 display: flex;
9 flex-direction: column;
10 align-items: center;
11 }
12 .userinfo-avatar {
13 width: 128rpx;
14 height: 128rpx;
15 margin: 20rpx;
16 border-radius: 50%;
17 }
18
19 .userinfo-nickname {
20 color: #aaa;
21 }
22
23 .usermotto {
24 margin-top: 200px;
25 }

三、配置index.js

 //index.js
//获取应用实例
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
tempFilePaths: [],
downloadPicturePath:''
},
/**
* 上传图片方法
*/
upload: function () {
let that = this;
wx.chooseImage({
count: 9, // 默认9
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: res => {
wx.showToast({
title: '正在上传...',
icon: 'loading',
mask: true,
duration: 1000
})
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
let tempFilePaths = res.tempFilePaths;
that.setData({
tempFilePaths: tempFilePaths
})
/**
* 上传完成后把文件上传到服务器
*/
var count = 0;
//上传文件
for (var i = 0; i < this.data.tempFilePaths.length;i++){
wx.uploadFile({
url: "http://*****/upload",//请求上传的url
filePath: tempFilePaths[i],
name: 'filename',
header: {
"Content-Type": "multipart/form-data"
},
success: function (res) {
count++;
//如果是最后一张,则隐藏等待中
if (count == tempFilePaths.length) {
wx.hideToast();
}
wx.showToast({
title: '上传成功',
icon: '',
mask: true,
duration: 1500
})
},
fail: function (res) {
wx.hideToast();
wx.showModal({
title: '错误提示',
content: '上传图片失败',
showCancel: false,
success: function (res) { }
})
}
});
}
}
})
},
/**
* 预览下载的图片
*/
preview_download_picture:function(){
wx.previewImage({
current: this.data.downloadPicturePath,
urls: this.data.downloadPicturePath,
})
},
/**
* 下载图片方法
*/
download:function(){
var that = this;
wx.downloadFile({
url:"http://******/download", //仅为示例,并非真实的资源
success: function (res) {
console.log(res)
// 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
if (res.statusCode === 200) {
wx.playVoice({
filePath: res.tempFilePath
})
wx.showToast({
title: '下载成功',
icon: '',
mask: true,
duration: 1500
})
that.setData({
downloadPicturePath: res.tempFilePath//将下载的图片路径传给页面显示
})
}
//保存下载的图片到本地
// wx.saveImageToPhotosAlbum({
// filePath: res.tempFilePath,
// success:
// function (data) {
// console.log(data);
// // wx.showModal({
// // title: '下载成功',
// // content: '下载成功',
// // })
// wx.showToast({
// title: '下载成功',
// icon: '',
// mask: true,
// duration: 1500
// })
// that.setData({
// downloadPicturePath: res.tempFilePath
// })
// },
// })
}
});
},
/**
* 预览图片方法
*/
listenerButtonPreviewImage: function (e) {
let index = e.target.dataset.index;
let that = this;
wx.previewImage({
current: that.data.tempFilePaths[index],
urls: that.data.tempFilePaths,
//这根本就不走
success: function (res) {
//console.log(res);
},
//也根本不走
fail: function () {
//console.log('fail')
}
})
}, /**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) { }, /**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () { }, /**
* 生命周期函数--监听页面显示
*/
onShow: function () { }, /**
* 生命周期函数--监听页面隐藏
*/
onHide: function () { }, /**
* 生命周期函数--监听页面卸载
*/
onUnload: function () { }, /**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () { }, /**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () { }, /**
* 用户点击右上角分享
*/
onShareAppMessage: function () { }
})

  

微信小程序上传与下载文件的更多相关文章

  1. 微信小程序上传Excel文本文件功能

    问题: 在开发过程中会发现微信小程序有很多功能都还不能满足我们的需求,谁叫客户就是上帝呢,前几天小编遇到了这么个问题,就是用微信小程序上传文件,但是还以为微信带有这个模块,可是查了许久还是没有找到,只 ...

  2. 微信小程序上传多张图片,及php后台处理

    微信小程序上传多张图片,级小程序页面布局直接来代码index.wxml <view class='body' style='width:{{windowWidth}}px;height:{{wi ...

  3. Taro 微信小程序 上传文件到minio

    小程序前端上传文件不建议直接引用minio的js npm包,一来是这个包本身较大,会影响小程序的体积,二来是ak sk需要放到前端存储,不够安全,因此建议通过请求后端拿到签名数据后上传. 由于小程序的 ...

  4. 微信小程序上传后发布或者体验版测试无数据解决办法

    在做微信小程序开发的过程中,发现小程序在本地调用接口的数据都显示,但是上传之后,发现手机体验没有数据.以下为解决办法: 1.先清除缓存试试. 2.打开微信小程序工具右上角的详情——项目设置,将“不校验 ...

  5. 微信小程序-上传照片-多张显示

    图片就是一个简单的效果 实现 先看wxml和wxss代码 <view class='in-demand'> <view class='dema-title'> <text ...

  6. 微信小程序-上传下载

    wx.uploadFile(OBJECT) 上传 将本地资源上传到开发者服务器.如页面通过 wx.chooseImage(图片)/wx.chooseVideo(视频) 等接口获取到一个本地资源的临时文 ...

  7. 微信小程序上传文件遇到的坑

    在开发小程序时,使用的花生壳做的内网映射,域名使用花生壳卖的https域名 在做小程序文件上传时,调用接口,老是报错. Caused by: org.apache.commons.fileupload ...

  8. 微信小程序上传Word文档、PDF、图片等文件

    <view class="main" style="border:none"> <view class="title"&g ...

  9. 微信小程序-上传多张图片加进度条(支持预览、删除)

    2018-12-24 详情示例见:https://www.cnblogs.com/cisum/p/9564898.html 2018-12-29 组件下载见:https://www.cnblogs.c ...

随机推荐

  1. Vue.js之Vue计算属性、侦听器、样式绑定

    前言 上一篇介绍了Vue的基本概念,这一篇介绍一下Vue的基本使用. 一.搭建一个Vue程序 1.1 搭建Vue环境 搭建Vue的开发环境总共有三种方法: 引入CDN <script src=& ...

  2. 使用DDL触发器同步多个数据库结构

    使用DDL触发器同步多个数据库结构 背景:当开发组比较大时,势必会分布到不同的地理位置,若无法在同一个快速网络中工作,就会造成多个开发库并存的局面,这样就需要多个开发库结构的同步,甚至是开发测试数据的 ...

  3. 三维计算机视觉 —— 中层次视觉 —— RCNN Family

    RCNN是从图像中检测物体位置的方法,严格来讲不属于三维计算机视觉.但是这种方法却又非常非常重要,对三维物体的检测非常有启发,所以在这里做个总结. 1.RCNN - the original idea ...

  4. linux系统关闭指定服务的方式

    1.根据名称称查找并关闭:pgrep -f 名称 | xargs kill -9 2.根据端口称查找并关闭:lsof -i:端口 | grep LISTEN|awk '{print $2}'|xarg ...

  5. J.U.C

  6. VUE组件的学习

    参考:https://blog.csdn.net/baidu_23142899/article/details/79130225

  7. Win10, VS2017环境下OpenCV3.4.2的配置

    从官网https://opencv.org/releases.html下载OpenCV3.4.2的Win pack进行安装,安装目录便如下图所示: 要能在Visual Studio中使用上述安装的Op ...

  8. mysql 查询排名 返回值拼接的骚操作

    故事背景: 依照原系统的框子搞一个新的系统给其他人使用,因为新的系统配置库依然需要使用原有的表,表中有字段区分新的系统,然后就有了这个很没劲的数据同步. 难点:配置库码表数据的主键之前是自增,后来改造 ...

  9. ubantu中怎样安装VMware Tools

    点击虚拟机选择安装VMware tools tar zxvf VMwareTools-9.6.0-1294478.tar.gz -C /root/(安装到的目录)cd /root/cd vmware- ...

  10. Javaweb实现对mongodb的增删改查(附带源代码)

    运行截图: 删除后的信息 项目源代码:https://www.cnblogs.com/post/readauth?url=/zyt-bg/p/9807396.html