前端(.wxml)

<view id="view1">
<view id="btns">
<image id="ima1" mode="aspectFitf" src="{{src}}"></image>
<button type="primary" bindtap="btntakephoto">拍摄照片</button>
<button type="primary" bindtap="btnchoosephoto">选择照片</button>
<button type="primary" bindtap="btnupload">上传</button>
</view>
</view>

样式(wxss)

button{
margin: 8rpx;
padding: 0rpx;
font-size: 30rpx;
width: 200rpx;
float: left;
}
#view1
{
width: %;
height: %;
}
#btns
{
margin: 50rpx;
padding: 0rpx;
}
image{
width: 650rpx;
height: 1050rpx;
background-color: lavender;
}

js(.js)

Page({
data: {
filepath:"",
},
onLoad: function (options) {
this.ctx = wx.createCameraContext()
},
//拍摄照片
btntakephoto: function () {
this.ctx.takePhoto({
quality: 'high',
success: (res) => {
this.setData({
src: res.tempImagePath,
filepath: res.tempImagePath[],
})
}
})
},
//选择照片
btnchoosephoto: function() {
wx.chooseImage({
count: , // 默认9
sizeType: ['original'],
sourceType: ['camera'],
success:(res) => {
this.setData({
src: res.tempFilePaths[],
filepath: res.tempFilePaths[],
});
}
})
},
//上传图片
btnupload: function () {
if (this.data.filepath == "")
{
wx.showToast({
title: '没有选择图片',
icon: 'none',
duration:
})
}
else
{
wx.uploadFile({
url: 'http://localhost:9965/api/image/WxPostFile',
filePath: this.data.filepath,
name: 'file',
formData: {
filename: ''
},
success(res) {
console.log(res);
wx.showToast({
title: "上传成功",
icon: 'success',
duration:
})
}
})
}
}
})

json配置(.json)

{
"navigationBarTitleText": "上传图片",
"navigationBarBackgroundColor": "#003a9b",
"navigationBarTextStyle": "white"
}

后台c#

 #region 测试微信小程序图片上传
[HttpPost]
public HttpResponseMessage WxPostFile()
{
bool isSuccess = false;
try
{
HttpPostedFile file = HttpContext.Current.Request.Files[];
var filename = HttpContext.Current.Request["filename"];
string path = AppDomain.CurrentDomain.BaseDirectory + "Out";
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
//var mapPath = HttpContext.Current.Server.MapPath(path); //硬盘物理目录
var fileExt = Path.GetExtension(file.FileName);//文件后缀名(.png)
var mapPathFullPath = path + "\\" + filename + fileExt; //硬盘物理路径
file.SaveAs(mapPathFullPath);
isSuccess = true;
}
catch (Exception ex)
{
isSuccess = false;
}
var resultObj = JsonConvert.SerializeObject(isSuccess);
HttpResponseMessage result = new HttpResponseMessage
{
Content = new StringContent(resultObj, Encoding.GetEncoding("UTF-8"), "application/json")
};
return result;
}
#endregion

本地测试

微信小程序上传图片及本地测试的更多相关文章

  1. H5本地存储技术和微信小程序中的本地存储

    1.H5的本地存储 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  2. 微信小程序 上传图片并等比列压缩到指定大小

    微信小程序官方API中  wx.chooseImage() 是可以进行图片压缩的,可惜的是不能压缩到指定大小. 实际开发中需求可能是压缩到指定大小: 原生js可以使用canvas来压缩,但由于微信小程 ...

  3. 微信小程序环境搭建(本地,测试,生产)

    1.本地 官网文档链接:https://cloud.tencent.com/document/product/619/11442#.E6.9C.AC.E5.9C.B0.E5.A6.82.E4.BD.9 ...

  4. 微信小程序如何进行本地调试

    1.下载并使用微信开发者工具 2.将项目导入工具 3.在项目中修改请求http地址 4.在工具上点击“测试号”,跳转到测试号管理界面设置request合法域名. 注意这里输入的测试域名要和上一步相同. ...

  5. 微信小程序上传图片(附后端代码)

    几乎每个程序都需要用到图片. 在小程序中我们可以通过image组件显示图片. 当然小程序也是可以上传图片的,微信小程序文档也写的很清楚. 上传图片 首先选择图片 通过wx.chooseImage(OB ...

  6. .NET开发微信小程序-上传图片到服务器

    1.上传图片分为几种: a:上传图片到本地(永久保存) b:上传图片到本地(临时保存) c:上传图片到服务器 a和b在小程序的api文档里面有.直接说C:上传图片到服务器 前端代码: /* 上传图片到 ...

  7. 微信小程序加载本地图片方法

    目录结构如下,只要图片按正确的方式放入小程序的开发工具的项目中,即可在wxml文件中用内联样式或者image标签都可以引用本地的图片. 步骤一:微信开发工具 打开项目 步骤二:新建个文件夹(放项目的一 ...

  8. (十)微信小程序---上传图片chooseImage

    官方文档 示例一 wxml <view bindtap="uploadImage">请上传图片</view> <image wx:for=" ...

  9. 微信小程序上传图片(前端+PHP后端)

    一.wxml文件 <text>上传图片</text> <view> <button bindtap="uploadimg">点击选择 ...

随机推荐

  1. Beta冲刺(1/4)

    队名:福大帮 组长博客链接: https://www.cnblogs.com/mhq-mhq/p/11990568.html 作业博客 : https://edu.cnblogs.com/campus ...

  2. Unity通过世界坐标系转换到界面坐标位置

    public static Vector2 WorldToCanvasPoint(Canvas canvas, Vector3 worldPos) { Vector2 pos; RectTransfo ...

  3. 【MyBatis】【SQL】没有最快,只有更快,从一千万条记录中删除八百万条仅用1分9秒

    这次直接使用delete from emp where cdate<'2018-02-02',看看究竟会发生什么. Mapper里写好SQL: <?xml version="1. ...

  4. 在业务控制方法中写入模型变量收集参数,且使用@InitBind来解决字符串转日期类型

    1)  在默认情况下,springmvc不能将String类型转成java.util.Date类型,所有我们只能在Action 中自定义类型转换器 <form action="${pa ...

  5. selenium 2019 笔记

    1.get打开本地目录的方法

  6. Delphi连接sql数据库怎么判断连接是否成功呢

    一切属性都设置好后,我在Button里 ADOConnection1.Connected := True;这样连的,之后如何判断是否连接成功了呢?查询一下数据试试?有更直接的办法吗? -------- ...

  7. Java日志体系(六)log4j2

    1.1 简介 log4j2,一个日志的实现框架,是log4j的升级版本,于2014年7月正式亮相.与第一代log4j不同,log4j2完全重写了log4j的日志实现,并不是在原有基础上进行的升级,解决 ...

  8. java数据结构之自定义队列

    一.队列的特点 1.队列是线性结构 2.先进先出,先进入队列的排在队列前端,会比后进队列的先出队列.FIFO 二.通过数组来实现队列 //自己实现数组队列,队列的特定就是先进先出 public cla ...

  9. Linux - curl和Wget

    curl - transfer a URL curl SYNOPSIS curl [options] [URL...] DESCRIPTION curl is a tool to transfer d ...

  10. STS中不同包但相同类名引起的问题:A component required a bean of type 'javax.activation.DataSource' that could not be found

    1. 问题输出: APPLICATION FAILED TO START*************************** Description: A component required a ...