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 @ 2023-05-09 18:40  212的s  阅读(835)  评论(0编辑  收藏  举报