前端(.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. HashMap简单介绍

    哈希表(hash table)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希表. 一.什么是哈希表 在讨论哈希表之 ...

  2. No suitable constructor was found in NUnit Parameterised tests

    No suitable constructor was found in NUnit Parameterised tests Fairly obvious, but can also happen i ...

  3. CCF 2017 09-02 公共钥匙盒

     CCF 2017 09-02 公共钥匙盒 1.用快速排序函数结合排序规则函数来给取放排序. 2.vector数组的强大功能. #include<iostream> #include< ...

  4. Appnium安装

    Refer to https://blog.csdn.net/xgh1951/article/details/85124327

  5. ObjectId初探

    ObjectId MongoDB每个集合存储的每个文档必须有一个"_id"键,默认是个ObjectId对象. "_id"作为当前文档在集合的唯一标识. 71st ...

  6. LAMP之Apache调优

    一.环境的搭建 实验环境: 操作系统:Centos 7.4 [root@xuegod70 ~]# ls apr-1.6.3.tar.gz apr-util-1.6.1.tar.bz2 httpd-2. ...

  7. go 语言 interface{} 的易错点

    一,interface 介绍 如果说 goroutine 和 channel 是 go 语言并发的两大基石,那 interface 就是 go 语言类型抽象的关键.在实际项目中,几乎所有的数据结构最底 ...

  8. centos7操作

    一.安装系统 1.下载(Minimal ISO)http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1 ...

  9. python基础知识(继承)

    继承的基本语法 class  Class(继承那个基类如果有多个基类用逗号隔开,如果没有就继承object): """ 类的帮助信息""" ...

  10. 如果Mysql插入字符始终乱码怎么办?

    1.找到my.cnf [root@Sliver-Code ~]# vi /etc/my.cnf 加入下面三行语句 [client] default-character-set=utf8 [mysqld ...