小程序上传图像失败

小程序端

javascript 复制代码
onChooseAvatar(e) {
    wx.showLoading({ title: '加载中' });
        const that = this; 

        const fs = wx.getFileSystemManager();
        fs.readFile({
          filePath: e.detail.avatarUrl,
          encoding: 'base64',
          success:(res) =>{
            wx.request({
              url: getApp().globalData.host + "User/UploadBase64",
              method: 'POST',
              //header: { 'content-type': 'application/json' },
              data: {
                data: res.data
              },
              success:(r) =>{ 
                this.setData({ avatarUrl: getApp().globalData.url + r.data })
                wx.showToast({ title: '上传成功', icon: 'none' })
             },
              fail(e) { console.error('base64 upload fail', e); }
            });
          },
          fail(e) { console.error('readFile fail', e); }
        });
  }

服务器端

cs 复制代码
[AllowAnonymous]
[HttpPost, Route("UploadBase64")]
public string UploadBase64(string data)
{
    try
    {
        if (string.IsNullOrEmpty(data)) throw new Exception("no data");
        var bytes = Convert.FromBase64String(data);
        string folder = "/Uploads/image";
        string save = Server.MapPath(folder);
        if (!Directory.Exists(save)) Directory.CreateDirectory(save);
        string file = Guid.NewGuid().ToString() + ".jpg";
        string full = Path.Combine(save, file);
        System.IO.File.WriteAllBytes(full, bytes);
        return Path.Combine(folder, file).Replace('\\', '/');
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}
相关推荐
淡海水7 小时前
07-02-源码-.NET Core GC分配路径
算法·unity·c#·.netcore·gc·1024程序员节
吴可可1239 小时前
C#调用摄像头拍照的三种API方案
c#
普通人614 小时前
uniapp模版JS
开发语言·javascript·小程序·uni-app·1024程序员节
Msir_Modern14 小时前
Noita 元胞沙盒笔记
算法·unity·c#·游戏引擎·元胞自动机
星之擎15 小时前
Task 很优雅的取消方式是什么?C# 里面。
开发语言·c#
andrsted16 小时前
企业培训时间难统一?练题簿让在线免费刷题与培训考核更好安排
学习·微信小程序·小程序·学习方法·刷题
czhc114007566317 小时前
一周概念复盘:预热、漂移与门禁
c#
李纲明17 小时前
微信小程序 wx.uploadFile 上传失败:合法域名、临时路径与 HTTPS 证书排查
微信小程序·小程序·uploadfile·devdiy·上传失败
云中飞鸿17 小时前
.Net和C#有什么关系?(从C++视角观察)
开发语言·c#·.net
hnxaoli19 小时前
win10小程序(二十四)批量新建文件夹
小程序