微信小程序上传图片及本地测试

前端(.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: 100%;
  height: 100%;
}
#btns
{
  margin: 0 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[0],
        })
      }
    })
  },
  //选择照片
  btnchoosephoto: function() {
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original'],
      sourceType: ['camera'],
      success:(res) => {
        this.setData({
          src: res.tempFilePaths[0],
          filepath: res.tempFilePaths[0],
        });
      }
    })
  },
  //上传图片
  btnupload: function () {
    if (this.data.filepath == "")
    {
      wx.showToast({
        title: '没有选择图片',
        icon: 'none',
        duration: 2000
      })
    }
    else
    {
      wx.uploadFile({
        url: 'http://localhost:9965/api/image/WxPostFile',
        filePath: this.data.filepath,
        name: 'file',
        formData: {
          filename: '123456789'
        },
        success(res) {
          console.log(res);
          wx.showToast({
            title: "上传成功",
            icon: 'success',
            duration: 2000
          })
        }
      })
    }
  }
})

json配置(.json)

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

后台c#

 #region 测试微信小程序图片上传
        [HttpPost]
        public HttpResponseMessage WxPostFile()
        {
            bool isSuccess = false;
            try
            {
                HttpPostedFile file = HttpContext.Current.Request.Files[0];
                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

本地测试

 

转载于:https://www.cnblogs.com/shuaimeng/p/10541020.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值