微信小程序图片上传并展示
1.首先编写微信小程序的页面和样式:
index.js
var total = [];
Page({
data: {
perImgSrc: []
},
onLoad: function (options) {
// 页面初始化 options为页面跳转所带来的参数
},
onReady: function () {
// 页面渲染完成
},
onShow: function () {
// 页面显示
},
onHide: function () {
// 页面隐藏
},
onUnload: function () {
// 页面关闭
},
chooseImg: function () {
var that = this;
wx.chooseImage({
count: 9, // 默认9
sizeType: ['original'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album'], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
var tempFilePaths = res.tempFilePaths;
console.info(res.tempFilePaths.length);
that.uploadFile2(tempFilePaths, 0);
}
})
}, uploadFile2: function (file, i) {//递归调用
var that = this;
wx.uploadFile({
url: 'http://localhost:8080/web/uploadImage', //仅为示例,非真实的接口地址
filePath: file[i],
name: 'file',
success: function (res) {
var obj = new Object();
obj.ind = i + 1;
var data = res.data;
var resultData = JSON.parse(res.data);
that.setData({
imageUrl: resultData.url
});
}
})
}
})
index.wxml
<text>单张图片上传</text>
<view>
<image src="{{imageUrl}}"></image>
</view>
<button bindtap="chooseImg">选择图片</button>
index.wxss
.btn{
margin-top: 10rpx;
}
然后是SpringMVC接收上传图片并回显示:
package com.tydic.www.controller; import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; import com.tydic.www.common.utils.FileUtil; @Controller
public class TestController { private static final Logger LOGGER = Logger.getLogger(TestController.class); @RequestMapping("/uploadImage")
@ResponseBody
public Object upload(HttpServletRequest request, @RequestParam("file")MultipartFile[] files){
LOGGER.info("上传测试");
Map<String,Object> map = new HashMap<>();
//多文件上传
if(files!=null && files.length>=1) {
BufferedOutputStream bw = null;
try {
String fileName = files[0].getOriginalFilename();
//判断是否有文件(实际生产中要判断是否是音频文件)
String UPLOADPATH = request.getSession().getServletContext().getRealPath("/images/");
if(!StringUtils.isEmpty(fileName)) {
//创建输出文件对象
String dirName = UUID.randomUUID().toString()+'.'+ FileUtil.getFileType(new File(fileName));
String dirPath = UPLOADPATH + dirName;
File outFile = new File(dirPath);
//拷贝文件到输出文件对象
FileUtils.copyInputStreamToFile(files[0].getInputStream(), outFile);
String url = request.getScheme() +"://" + request.getServerName() + ":" +request.getServerPort() + "/web/images/"+dirName;
System.out.println(url);
map.put("url", url);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(bw!=null) {
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} return map;
}
}
返回的结果直接展示在小程序里:
微信小程序图片上传并展示的更多相关文章
- 微信小程序图片上传和裁剪
本篇博客用于解决微信小程序图片裁剪问题 图片裁剪常用于头像选择和图片合成等. 图片裁剪解决方案: 目前网络上知名的微信小程序图片裁剪插件是we-cropper(文末有链接) 操作步骤:下载好we-cr ...
- 5行代码实现微信小程序图片上传与腾讯免费5G存储空间的使用
本文介绍了如何在微信小程序开发中使用腾讯官方提供的云开发功能快速实现图片的上传与存储,以及介绍云开发的 5G 存储空间的基本使用方法,这将大大提高微信小程序的开发效率,同时也是微信小程序系列教程的视频 ...
- 快速高效实现微信小程序图片上传与腾讯免费5G存储空间的使用
本文介绍了如何在微信小程序开发中使用腾讯官方提供的云开发功能快速实现图片的上传与存储,以及介绍云开发的 5G 存储空间的基本使用方法,这将大大提高微信小程序的开发效率 对于一般的图片上传功能开发,我们 ...
- 微信小程序---图片上传+服务端接受
原文地址:http://blog.csdn.net/sk719887916/article/details/54312573 微信小程序,图片上传,应用地方-修改用户信息的头像. 详细代码: 小程序的 ...
- 微信小程序图片上传java后台(前后端代码)
小程序代码 upload:function(e){ var that = this; wx.showActionSheet({ itemList: ['从相册选择','拍照'], itemColor: ...
- 微信小程序图片上传放大预览删除代码
效果: 一,下面是上传图片的效果 image.js代码: Page({ //选择相册或拍照 data: { imgs: [] }, //上传图片 chooseImg: function (e) { v ...
- 微信小程序图片上传
uploadImage : function (){ wx.chooseImage({ count: 9, // 默认9 sizeType: ['original', 'compressed'], / ...
- (SSM框架)实现小程序图片上传(配小程序源码)
阅读本文约"2分钟" 又是一个开源小组件啦! 因为刚好做到这个小功能,所以就整理了一下,针对微信小程序的图片(文件)上传! 原业务是针对用户反馈的图片上传.(没错,本次还提供小程序 ...
- 小程序--->小程序图片上传阿里OSS使用方法
小程序图片上传阿里OSS使用方法 首先看下参考文档 ( http://blog.csdn.net/qq_38125123/article/details/73870667) 这里只将一些运用过程中遇到 ...
随机推荐
- Log stash学习笔记(一)
Logstash是一款开源的数据收集引擎,具备实时管道处理能力.简单来说,logstash作为数据源与数据存储分析工具之间的桥梁,结合 ElasticSearch以及Kibana,能够极大方便数据的处 ...
- 配置SSD-caffe测试时出现“Check failed: error == cudaSuccess (10 vs. 0) invalid device ordinal”解决方案
这是由于GPU数量不匹配造成的,如果训练自己的数据,那么我们只需要将solver.prototxt文件中的device_id项改为自己的GPU块数,一块就是0,两块就是1,以此类推. 但是SSD配置时 ...
- git别名;git配置使用shell函数;git别名使用shell函数;git获取当前分支;git alias
获取当前分支 git symbolic-ref -q --short HEAD 2. 在git别名里使用shell函数,$1获取第一个参数的值,$2……$n依次类推,根据自己习惯需要定制 3. 提交r ...
- 使用idea 在springboot添加本地jar包的方法本地运行有效,一旦需要打jar就会报错,这就需要在
https://blog.csdn.net/huxiaodong1994/article/details/80702278 1.首先在与src同级的目录下新建一个lib目录,然后将本地jar包放在li ...
- input type=file 选择图片并且实现预览效果的实例
为大家带来一篇input type=file 选择图片并且实现预览效果的实例. 通过<input />标签,给它指定type类型为file,可提供文件上传: accept:可选择上传类型, ...
- 7.1 服务暴露前的准备-ServiceBean的装配
dubbo的服务暴露以第一章 第一个dubbo项目中的dubbo-demo-provider来讲述. 列出dubbo-demo-provider的xml配置: <?xml version=&qu ...
- 用 .NET Memory Profiler 跟踪.net 应用内存使用情况--基本应用篇(转)
.net 框架号称永远不会发生内存泄漏,原因是其引入了内存回收的机制.但实际应用中,往往我们分配了对象但没有释放指向该对象的引用,导致对象永远无法释放.最 常见的情况就是给对象添加了事件处理函数,但当 ...
- ubuntu18.04+ cuda9.0+opencv3.1+caffe-ssd安装
详细Ubuntu18.04,CUDA9.0,OpenCV3.1,Tensorflow完全配置指南 问题1:使用Cmake编译opencv源码 CMake Error: The following va ...
- Construct Binary Tree from Inorder and Postorder Traversal Traversal leetcode java
题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...
- Jquery怎么获取select选中项 自定义属性的值
Jquery如何获取select选中项 自定义属性的值?HTML code <select id="ddl" onchange="ddl_change(this)& ...