微信小程序上传与下载文件
需要准备的工作:
①、建立微信小程序工程,编写以下代码。
②、通过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 () { }
})
微信小程序上传与下载文件的更多相关文章
- 微信小程序上传Excel文本文件功能
问题: 在开发过程中会发现微信小程序有很多功能都还不能满足我们的需求,谁叫客户就是上帝呢,前几天小编遇到了这么个问题,就是用微信小程序上传文件,但是还以为微信带有这个模块,可是查了许久还是没有找到,只 ...
- 微信小程序上传多张图片,及php后台处理
微信小程序上传多张图片,级小程序页面布局直接来代码index.wxml <view class='body' style='width:{{windowWidth}}px;height:{{wi ...
- Taro 微信小程序 上传文件到minio
小程序前端上传文件不建议直接引用minio的js npm包,一来是这个包本身较大,会影响小程序的体积,二来是ak sk需要放到前端存储,不够安全,因此建议通过请求后端拿到签名数据后上传. 由于小程序的 ...
- 微信小程序上传后发布或者体验版测试无数据解决办法
在做微信小程序开发的过程中,发现小程序在本地调用接口的数据都显示,但是上传之后,发现手机体验没有数据.以下为解决办法: 1.先清除缓存试试. 2.打开微信小程序工具右上角的详情——项目设置,将“不校验 ...
- 微信小程序-上传照片-多张显示
图片就是一个简单的效果 实现 先看wxml和wxss代码 <view class='in-demand'> <view class='dema-title'> <text ...
- 微信小程序-上传下载
wx.uploadFile(OBJECT) 上传 将本地资源上传到开发者服务器.如页面通过 wx.chooseImage(图片)/wx.chooseVideo(视频) 等接口获取到一个本地资源的临时文 ...
- 微信小程序上传文件遇到的坑
在开发小程序时,使用的花生壳做的内网映射,域名使用花生壳卖的https域名 在做小程序文件上传时,调用接口,老是报错. Caused by: org.apache.commons.fileupload ...
- 微信小程序上传Word文档、PDF、图片等文件
<view class="main" style="border:none"> <view class="title"&g ...
- 微信小程序-上传多张图片加进度条(支持预览、删除)
2018-12-24 详情示例见:https://www.cnblogs.com/cisum/p/9564898.html 2018-12-29 组件下载见:https://www.cnblogs.c ...
随机推荐
- adb logcat查看某个进程的输出日志
adb logcat查看某个进程的输出日志 adb logcat 默认是没有这个功能的,我实现了一个小bash函数,添加到你$HOME/.bashrc 文件中: # 作用:能够通过进程名显示log # ...
- 搜素表脚本.vbs
Set oFso = CreateObject("Scripting.FileSystemObject")dim path(30)dim name(30)'说明书表头有15列:补丁 ...
- nw.js---创建一个hello word的方法
一.如果用nw.js 来开发桌面应用 首先到Nw.js中文网下载软件: https://nwjs.org.cn/download.html 下载下来进行解压就可以了,绿色的免安装的,整个目录结果是这样 ...
- Solve Error: nafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new[](unsigned int)" (??_U@YAPAXI@Z) already defined in libcpmt.lib(newaop.obj)
Error: nafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new[](unsigned int)&quo ...
- 腾讯地图打开地图选取位置 withMap
https://lbs.qq.com/tool/component-picker.html withMap import React, { Component } from "react&q ...
- DCL并非单例模式专用
我相信大家都很熟悉DCL,对于缺少实践经验的程序开发人员来说,DCL的学习基本限制在单例模式,但我发现在高并发场景中会经常遇到需要用到DCL的场景,但并非用做单例模式,其实DCL的核心思想和CopyO ...
- kill、killall、pkill杀手三人组
1.1 kill.killall.pkill杀手三人组 1.利用kill 进程号 方式杀掉rsync进程 [root@backup ~]# ps -ef |grep rsync root 3500 1 ...
- Python学习之旅(二十五)
Python基础知识(24):正则表达式 正则表达式:检查一个字符串是否与某个模式匹配 \d :匹配数字 \w :匹配字母或数字 . :匹配任意字符 {n} :匹配n个字符 {m,n} :匹配m到n个 ...
- oracle索引分类
参考文档:https://wenku.baidu.com/view/d4d6ee1ba76e58fafab00336.html https://blog.csdn.net/u010719917/art ...
- 把vim改装为source sight
本文在ubuntu18.04上实践. 主要为VIM 安装4个插件: taglist,srcexpl,NERD_tree,ctrlp 1,taglist.vim :https://www.vim.org ...