wx.uploadFile .net core接收

微信提供的文件上传方法:将本地资源上传到服务器。客户端发起一个 HTTPS POST 请求,其中 content-type 为 multipart/form-data

参数

Object object

属性类型默认值必填说明最低版本
url string   开发者服务器地址  
filePath string   要上传文件资源的路径 (本地路径)  
name string   文件对应的 key,开发者在服务端可以通过这个 key 获取文件的二进制内容  
header Object   HTTP 请求 Header,Header 中不能设置 Referer  
formData Object   HTTP 请求中其他额外的 form data  
timeout number   超时时间,单位为毫秒 2.10.0
success function   接口调用成功的回调函数  
fail function   接口调用失败的回调函数  
complete function   接口调用结束的回调函数(调用成功、失败都会执行)  

object.success 回调函数

参数
Object res
属性类型说明
data string 开发者服务器返回的数据
statusCode number 开发者服务器返回的 HTTP 状态码

返回值

UploadTask

基础库 1.4.0 开始支持,低版本需做兼容处理

一个可以监听上传进度进度变化的事件和取消上传的对象

微信官方给出的代码如下:

wx.chooseImage({
  success (res) {
    const tempFilePaths = res.tempFilePaths
    wx.uploadFile({
      url: 'https://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址
      filePath: tempFilePaths[0],
      name: 'file',
      formData: {
        'user': 'test'
      },
      success (res){
        const data = res.data
        //do something
      }
    })
  }
})

后台服务使用.NET CORE 3.1 

[HttpPost("/File/WeChatFile")]
public BaseOutput<string> WeChatFile()
{
         var file = HttpContext.Request.Form.Files[0];
         //do something
return new BaseOutput<string>() { Data = ""}; }

记录:之前一直使用IFormFile 来接收文件,微信这个方法用IFormFile 接收不到,从HttpContext.Request.Form中可以接收到参数

微信服务地址:https://developers.weixin.qq.com/miniprogram/dev/api/network/upload/wx.uploadFile.html

原文地址:https://www.cnblogs.com/ningyouyou/p/13445149.html