小程序上传图像失败

小程序端

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;
    }
}
相关推荐
2501_916007472 小时前
前端开发常用软件与工具全面指南
android·ios·小程序·https·uni-app·iphone·webview
xiaoshuaishuai86 小时前
C# AvaloniaUI 资源找不到报错
java·服务器·前端·windows·c#
Xin_ye100866 小时前
C# 零基础到精通教程 - 第十八章:部署与发布——让应用上线
开发语言·c#
爱讲故事的7 小时前
操作系统第一讲复习:为什么学习操作系统,以及操作系统到底在做什么?
linux·开发语言·windows·学习·ubuntu·c#
2501_915909067 小时前
iOS应用性能优化:十大策略提升用户体验与开发效率
android·ios·小程序·https·uni-app·iphone·webview
JaydenAI8 小时前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
z落落9 小时前
C# 继承:父子构造函数 + base 关键字 +五大访问修饰符(同项目+跨项目 全覆盖)
开发语言·c#
海盗12349 小时前
C#中PDF操作-QuestPDF页面设置与布局
java·pdf·c#
玩c#的小杜同学10 小时前
一周 AI 新鲜事|2026.05.25—2026.05.31
人工智能·程序人生·ai·c#·程序员创富