微信小程序上传图片及本地测试
前端(.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
本地测试
微信小程序上传图片及本地测试的更多相关文章
- H5本地存储技术和微信小程序中的本地存储
1.H5的本地存储 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- 微信小程序 上传图片并等比列压缩到指定大小
微信小程序官方API中 wx.chooseImage() 是可以进行图片压缩的,可惜的是不能压缩到指定大小. 实际开发中需求可能是压缩到指定大小: 原生js可以使用canvas来压缩,但由于微信小程 ...
- 微信小程序环境搭建(本地,测试,生产)
1.本地 官网文档链接:https://cloud.tencent.com/document/product/619/11442#.E6.9C.AC.E5.9C.B0.E5.A6.82.E4.BD.9 ...
- 微信小程序如何进行本地调试
1.下载并使用微信开发者工具 2.将项目导入工具 3.在项目中修改请求http地址 4.在工具上点击“测试号”,跳转到测试号管理界面设置request合法域名. 注意这里输入的测试域名要和上一步相同. ...
- 微信小程序上传图片(附后端代码)
几乎每个程序都需要用到图片. 在小程序中我们可以通过image组件显示图片. 当然小程序也是可以上传图片的,微信小程序文档也写的很清楚. 上传图片 首先选择图片 通过wx.chooseImage(OB ...
- .NET开发微信小程序-上传图片到服务器
1.上传图片分为几种: a:上传图片到本地(永久保存) b:上传图片到本地(临时保存) c:上传图片到服务器 a和b在小程序的api文档里面有.直接说C:上传图片到服务器 前端代码: /* 上传图片到 ...
- 微信小程序加载本地图片方法
目录结构如下,只要图片按正确的方式放入小程序的开发工具的项目中,即可在wxml文件中用内联样式或者image标签都可以引用本地的图片. 步骤一:微信开发工具 打开项目 步骤二:新建个文件夹(放项目的一 ...
- (十)微信小程序---上传图片chooseImage
官方文档 示例一 wxml <view bindtap="uploadImage">请上传图片</view> <image wx:for=" ...
- 微信小程序上传图片(前端+PHP后端)
一.wxml文件 <text>上传图片</text> <view> <button bindtap="uploadimg">点击选择 ...
随机推荐
- 20182303 2019-2020-1 《数据结构与面向对象程序设计》第2&3周学习总结
目录 教材学习内容总结 教材学习中的问题和解决过程 代码调试中的问题和解决过程 代码托管 上周考试错题总结 结对及互评 点评 学习进度条 教材学习内容总结 教材第二章内容 学习Java基本数据类型以及 ...
- Flutter移动电商实战 --(13)ADBanner组件的编写
1.AdBanner组件的编写 我们还是把这部分单独出来,需要说明的是,这个Class你也是可以完全独立成一个dart文件的.代码如下: 广告图片 class AdBanner extends Sta ...
- dubbo异常filter
dubbo请求调用过程分析 https://blog.csdn.net/javahongxi/article/details/72876694 浅谈dubbo的ExceptionFilter异常处理 ...
- 008-ICMP协议(网络控制文协议)
一.概述 ICMP是(Internet Control Message Protocol)Internet控制报文协议.它是TCP/IP协议簇的一个子协议,用于在IP主机.路由器之间传递控制消息.控制 ...
- "挡位"还是"档位",究竟谁错了
http://baijiahao.baidu.com/s?id=1581395663965196858&wfr=spider&for=pc 对于“挡”与“档”两个字,我一直并没有给以太 ...
- vue build后 less sass background-img找不到文件
修改build下面的utils.js配置 if (options.extract) { return ExtractTextPlugin.extract({ use: loaders, publicP ...
- 在HbuilderX的Uni-app中引入crypto-js md5
在 hbuilderX的菜单“视图”中选择“显示终端”,在终端中把当前目录定位到uni-app的根目录,这样才可以把crypto-js库安装在正确的位置,在终端中键入:npm install cryp ...
- Visual Studio Code的设置及插件同步
Visual Studio Code的设置及插件同步 使用Visual Studio Code开发有一段时间了,用起来是极其的顺手,但是唯独一点不爽的就是,Visual Studio Code不像Vi ...
- linux 中的命令是什么?执行命令的几种方式?如何自己创建命令?
linux 中的命令是什么? 命令是可执行的二进制程序 执行命令的几种方式? ./test.sh #相对路径执行 /data/test.sh ...
- 【VS开发】ConvertBSTRToString(filename) 不能将string转换为BSTR
环境:win7,x64,vs2008 sp1 把VC 6.0的工程文件用VS2008打开,编译报错: error C2664:"_com_util::ConvertBSTRToString& ...