// 小程序端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