MVC Api微信小程序wx.uploadFile上传文件,前后端代码实例

转载地址https://blog.csdn.net/smartsmile2012/article/details/83574529

// 小程序端js
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    userHeaderImage: "../../../images/1.jpg"
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
 
  },
 
  chooseImage: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths[0]
        that.setData({
          userHeaderImage: tempFilePaths
        })
        console.log(tempFilePaths)
        //上传图片
        wx.uploadFile({
          url: 'https://xxxx/api/wxtest/uploadfile', //仅为示例,非真实的接口地址
          filePath: tempFilePaths,
          name: 'file',
          // header: {
          //   "content-type": "multipart/form-data",
          //   'content-type': 'application/x-www-form-urlencoded' //表单提交
          // },
          formData: {
            'userId': 10001
          },
          success(res) {
            const data = res.data
            console.log(res);
            //do something
          }
        })
      }
    })
  },
})
index.wxml
<view class='mainView' bindtap='chooseImage'>
  <image class="imageClass" src="{{userHeaderImage}}"></image>
</view>
#region 测试微信小程序图片上传
        [Route("api/wxtest/uploadfile")] //设置路由
        [HttpPost]
        public HttpResponseMessage WxPostFile()
        {
            //定义
            ResponseResult obj = new ResponseResult();
            try
            {
                HttpPostedFile file = HttpContext.Current.Request.Files[0];
                var userId = HttpContext.Current.Request["userId"];
                var path = "/upload/";
                var mapPath = HttpContext.Current.Server.MapPath(path); //硬盘物理目录
                var fileName = Until.GetNowTimeString();
                var fileExt = Path.GetExtension(file.FileName);
                var mapPathFullPath = mapPath + fileName + fileExt; //硬盘物理路径
                var siteFullPath = path + fileName + fileExt; //网站路径
                if (!Directory.Exists(mapPath))
                {
                    Directory.CreateDirectory(mapPath);
                }
                file.SaveAs(mapPathFullPath);
                obj.status = true;
                obj.message = siteFullPath;
                obj.info = userId;
            }
            catch (Exception ex)
            {
                obj.status = false;
                obj.message = ex.Message;
            }
            var resultObj = JsonConvert.SerializeObject(obj);
            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(resultObj, Encoding.GetEncoding("UTF-8"), "application/json")
            };
            return result;
        }
        #endregion

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值