uniapp微信小程序,上传图片和表单数据一起提交

弄表单上传,请求一直400错误,然后寻找其它方法
 uni.uploadFile({
                        url: 'http://localhost:6109/api/My/warrantyCard', // 上传服务器的 URL
                        filePath: this.ImgUrl , // 本地图片的路径或临时文件路径
                        name: 'InvoiceFile', // 上传文件的名称
                        success: function (res) {
                          console.log(res.data); // 上传成功后的文件 URL
                        }
                      });
后端:

 [HttpPost]
        public IActionResult warrantyCard(IFormFile InvoiceFile)
        {

}

 

这种可以单独传一个文件,如果加上表单其它内容,就提示http  415

分两步,很麻烦


base64的提交方式:

this.imagUrl 是上传组件 <uni-file-picker  @select 事件的参数得到的

 const InvoiceFile = uni.getFileSystemManager().readFileSync(this.ImgUrl, 'base64');

然后就普通 post请求,InvoiceFile 放data里提交

复制代码
后端:


 [HttpPost]
        public IActionResult warrantyCard(Data443 data)
        {

            if (string.IsNullOrEmpty(data.InvoiceFile) == false)
            {
                byte[] binaryData = Convert.FromBase64String(data.InvoiceFile);
                string filePath = "/file.png";
                System.IO.File.WriteAllBytes(AppContext.BaseDirectory + filePath, binaryData);
            }
            return new JsonResult(new { msg = "ok" });
        }
复制代码

 

posted @   212的s  阅读(1137)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示