入坑微信小程序必经之路(六)图片上传服务器——WebSercice接口

wxml文件
<view class="weui-uploader">
<view class="img-v weui-uploader__bd">
<view class='pic' wx:for="{{imgs}}" wx:for-item="item" wx:key="*this">
<image class='weui-uploader__img'
src="{{urlimg}}{{item}}"
data-index="{{index}}" mode="aspectFill" bindtap="previewImg">
<icon type='cancel' class="delete-btn" data-index="{{index}}" catchtap="deleteImg"></icon>
</image>
</view> <!-- 用来提示用户上传图片 -->
<view class="weui-uploader__input-box pic" bindtap="chooseImg"> </view>
</view>
<button class="upload-img-btn" bindtap="chooseImg" type='primary' style="background-color:#ffcb63;">拍照 / 上传</button>
</view>
CSS文件
.img{
display: inline-block;
}
.pic {
float:left;
position:relative;
margin-right:9px;
margin-bottom:9px;
}
.delete-btn{
position: absolute;
top: 0;
right: 0;
}
.weui-uploader{
padding: 10rpx;
}
.lineHeight {
width: 100%;
line-height: 80rpx;
border-bottom: 1px solid #ccc;
font-size: 30rpx;
}
.container {
padding: 0;
align-items: left;
padding-left: 15rpx;
}
.numberInfo {
font-size: 24rpx;
text-indent: 15rpx;
border-bottom: 1px solid #ccc;
}
/* .input {
display: inline-block;
border: 1px solid #ccc;
line-height: 80rpx;
vertical-align: middle;
margin-left: 11%;
width: 75%;
} */
.input,
.input-7 ,
.input-15{
margin-left: 7%;
display: inline-block;
/* border: 1px solid #ccc; */
line-height: 80rpx;
vertical-align: middle;
width: 75%;
}
.input{
margin-left: 11%;
}
button {
width: 100%;
margin-top: 30rpx;
}
.select{
margin-left: 7%;
color: #666;
}
.input-15{
margin-left:15%;
}
JS文件——data自理
// 上传图片
chooseImg: function (e) {
var that = this;
var imgs = this.data.imgs;
if (imgs.length >= 9) {
this.setData({
lenMore: 1
});
setTimeout(function () {
that.setData({
lenMore: 0
});
}, 2500);
return false;
}
wx.chooseImage({
// count: 1, // 默认9
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
var tempFilePaths = res.tempFilePaths;
var imgs = that.data.imgs;
// console.log(tempFilePaths + '----');
for (var i = 0; i < tempFilePaths.length; i++) {
that.setData({
jstone:that.data.jstone+1,
})
var js=that.data.jstone;
if (imgs.length >= 9) {
that.setData({
imgs: imgs
});
that.upLoad(tempFilePaths[i],js);
return false;
} else {
that.upLoad(tempFilePaths[i],js);
imgs.push(tempFilePaths[i]);
}
}
// console.log(imgs);
that.setData({
imgs: imgs
});
console.log(that.data.imgsone);
}
});
},
upLoad(filePath,index){//上传图片到服务器
var url=app.globalData.url+"img_upload";
wx.showToast({
icon: "loading",
title: "正在上传"
}),
wx.uploadFile({
url: url, //服务器路径
filePath: filePath, //图片文件
name: 'file',
formData: null,
header: {
'content-type':'application/json'
},
success: res => {
// console.log(index);
this.setData({
['imgsone['+ index +']']:JSON.parse(res.data),
})
// console.log(JSON.parse(res.data));
// console.log(this.data.imgsone);
}
})
}, // 删除图片
deleteImg: function (e) {
var imgs = this.data.imgs;
var index = e.currentTarget.dataset.index;
imgs.splice(index, 1);
this.setData({
imgs: imgs
});
},
// 预览图片
previewImg: function (e) {
//获取当前图片的下标
var index = e.currentTarget.dataset.index;
//所有图片
var imgs = this.data.imgs;
console.log(imgs);
wx.previewImage({
//当前显示图片
current: imgs[index],
//所有图片
urls:imgs
})
},
WebService接口文件
[WebMethod]//上传图片
public void img_upload()//接收图片
{
string result = string.Empty;//返回图片路径
string fileExtension;//文件扩展名
string shijian;//当前时间
string newname;//新文件名
string currentpath;//网站根目录
try
{
string path = "/UpLoadFilesimages/";
HttpPostedFile file = System.Web.HttpContext.Current.Request.Files["file"]; //对应小程序 name //获取文件
if (file != null)
{
Stream sr = file.InputStream; //文件流
Bitmap bitmap = (Bitmap)Bitmap.FromStream(sr);
//取得文件的扩展名,并转换成小写
fileExtension = System.IO.Path.GetExtension(file.FileName).ToLower();
//获取当前时间
//shijian = DateTime.Now.ToLocalTime().ToString("yyyyMMddhhssmm");
shijian = string.Format("{0}{1}", DateTime.Now.ToString("yyyyMMddHHmmss"), GetUniqueKey1());
//合成新产生的文件名
newname = shijian + fileExtension;
path += newname;
currentpath = System.Web.HttpContext.Current.Server.MapPath("~"); bitmap.Save(currentpath + path);
}
result = path;//返回图片路径
}
catch (Exception vErr)
{
result = vErr.Message;
}
Context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
Context.Response.End(); }
public string GetUniqueKey1()
{
int maxSize = 8;
int minSize = 5;
char[] chars = new char[62];
string a;
a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
chars = a.ToCharArray();
int size = maxSize;
byte[] data = new byte[1];
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data);
size = maxSize;
data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size);
foreach (byte b in data)
{
result.Append(chars[b % (chars.Length - 1)]);
}
return result.ToString();
}
可上传多张到服务器
入坑微信小程序必经之路(六)图片上传服务器——WebSercice接口的更多相关文章
- 微信小程序简单封装图片上传组件
微信小程序简单封装图片上传组件 希望自己 "day day up" -----小陶 我从哪里来 在写小程序的时候需要上传图片,个人觉得官方提供的 Uploader 组件不是太好用, ...
- 微信小程序:多张图片上传
最近在写小程序的相册,需要多张图片的上传.因为小程序不支持数组的多张图片同时上传,然后根据自己的需求+借鉴网上各位大神的案例,总算搞定.分享下,不足之处,多多指教哦 页面wxml: <form ...
- 微信小程序--更换用户头像/上传用户头像/更新用户头像
changeAvatar:function (){ var that=this; wx.chooseImage({ count: 1, // 默认9 sizeType: ['original', 'c ...
- 微信小程序入门八头像上传
1. action-sheet 底部弹出可选菜单组件 2. wx.uploadFile 将本地资源上传到服务器 3. wx.chooseImage 从本地相册选择图片或使用相机拍照. 4. wx.pr ...
- 微信小程序红包开发 小程序发红包 开发过程中遇到的坑 微信小程序红包接口的
微信小程序红包开发 小程序发红包 开发过程中遇到的坑 微信小程序红包接口的 最近公司在开发一个小程序红包系统,客户抢到红包需要提现.也就是通过小程序来给用户发红包. 小程序如何来发红包呢?于是我想 ...
- 记账本微信小程序开发六
记账本微信小程序开发六 我的界面 主界面
- 微信小程序点击图片放大预览
微信小程序点击图片放大预览使用到 wx.previewImage 接口,可以放大.上/下一张 上代码 wxml代码 <view class='content-img' wx:if="{ ...
- 微信小程序 在canvas画布上划动,页面禁止滑动
要实现微信小程序 在canvas画布上划动,页面禁止滑动,不仅要设置disable-scroll="true",还要要给canvas绑定一个触摸事件才能生效. <canvas ...
- 微信小程序、SSL证书、开启服务器TSL1.0、TSL1.1、TSL1.2服务
微信小程序.SSL证书.开启服务器TSL1.0.TSL1.1.TSL1.2服务 https://blog.csdn.net/qq_32933615/article/details/70143105
随机推荐
- 详细讲讲netty的pipiline!
前言 提到 Netty 首当其冲被提起的肯定是支持它承受高并发的线程模型,说到线程模型就不得不提到 NioEventLoopGroup 这个线程池,接下来进入正题. 线程模型 首先来看一段 Netty ...
- 一看就会的高效Discuz初始化入门安装方法
在使用Discuz搭建论坛的过程中,小九发现有许多朋友对于宝塔的安装和初始化不太熟悉,找不到适合的方法.或是按照一些教程安装却出现问题得不到解决,只能选择重新再来. 今天,小九给大家介绍简单的镜像一键 ...
- DNS的正向解析与反向解析
DNS域名解析服务(Domain Name System)是用于解析域名与IP地址对应关系的服务,功能上可以实现正向解析与反向解析: 正向解析:根据主机名(域名)查找对应的IP地址. 反向解析:根据I ...
- 虚拟机下安装Centos设置静态ip,并通过桥接连接
Centos7.6设置静态IP 1.CentOS7默认网卡设备文件存放于该目录下 /etc/sysconfig/network-scripts/ 网卡设备默认的名字为:ifcfg-ens33 查看网卡 ...
- mongoose报错:DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead
参考:mongoose报错:DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead mo ...
- 接口和包--Java学习笔记
接口 定义及基础用法 interface定义:没有字段的抽象类 interface person{ void hello(); String getName(); } /*接口本质上就是抽象类 abs ...
- 容器平台(kubernetes)架构设计
一眨眼距离上次发文好几年过去了,今天翻未读邮件看到博客有文章回复,猛然想起将博客遗忘在角落好几年了,赶紧访问博客.找回密码.翻翻文章,想写点什么但是又不知道从哪下手,N年前的第一篇文章是一个crm设计 ...
- SpringBoot快速入门(必知必会)
是什么?能做什么 SpringBoot必知必会 是什么?能做什么 SpringBoot是一个快速开发脚手架 快速创建独立的.生产级的基于Spring的应用程序 SpringBoot必知必会 快速创建应 ...
- 网页前端video播放m3u8(HLS)
网页前端video播放m3u8(HLS) HLS (HTTP Live Streaming)是Apple公司研发的流媒体传输技术,包括一个m3u8的索引文件.多个ts分片文件和key加密串文件.这项技 ...
- Python - 面向对象编程 - 类变量、实例变量/类属性、实例属性
什么是对象和类 https://www.cnblogs.com/poloyy/p/15178423.html 什么是 Python 类.类对象.实例对象 https://www.cnblogs.com ...