微信小程序上传图片c# asp.net mvc端接收案例

在微信小程序上传图片到服务器,并在ASP.NET MVC后端接收这个图片,可以通过以下步骤实现:

1. 微信小程序端

首先,在微信小程序前端,使用 wx.chooseImage API 选择图片,然后使用 wx.uploadFile API 将图片上传到服务器。这里是一个简单的示例:

复制代码
wx.chooseImage({  
  count: 1, // 默认9  
  sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有  
  sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有  
  success(res) {  
    const tempFilePaths = res.tempFilePaths;  
    wx.uploadFile({  
      url: 'https://your-server-url/upload', // 替换为你的服务器上传接口地址  
      filePath: tempFilePaths[0],  
      name: 'file', // 与后端Controller中的参数名称对应  
      formData: {  
        'user': 'someone' // 其他表单数据,可选  
      },  
      success(uploadRes) {  
        if (uploadRes.statusCode !== 200) {  
          wx.showToast({  
            icon: 'none',  
            title: '上传失败'  
          });  
        } else {  
          wx.showToast({  
            title: '上传成功',  
            icon: 'success',  
            duration: 2000  
          });  
          // 处理上传成功后的逻辑,如跳转到其他页面等  
        }  
      },  
      fail(error) {  
        wx.showToast({  
          icon: 'none',  
          title: '上传失败:' + error.errMsg  
        });  
      }  
    });  
  }  
});

2. ASP.NET MVC后端

在ASP.NET MVC后端,你需要创建一个Controller来处理上传的文件。以下是一个简单的Controller示例:

复制代码
using System;  
using System.IO;  
using System.Web;  
using System.Web.Mvc;  
  
namespace YourNamespace.Controllers  
{  
    public class UploadController : Controller  
    {  
        // POST api/upload  
        [HttpPost]  
        public ActionResult Upload()  
        {  
            if (Request.Files.Count > 0)  
            {  
                var file = Request.Files[0]; // 获取上传的文件  
                if (file != null && file.ContentLength > 0)  
                {  
                    var fileName = Path.GetFileName(file.FileName); // 获取文件名(不包含路径)  
                    var filePath = Path.Combine(Server.MapPath("~/uploads"), fileName); // 指定保存路径,这里假设在web项目的uploads文件夹下  
  
                    // 检查文件是否已经存在  
                    if (!System.IO.File.Exists(filePath))  
                    {  
                        file.SaveAs(filePath); // 保存文件到服务器  
                        return Json(new { success = true, message = "文件上传成功", fileName = fileName, filePath = filePath }, JsonRequestBehavior.AllowGet);  
                    }  
                    else  
                    {  
                        return Json(new { success = false, message = "文件已存在" }, JsonRequestBehavior.AllowGet);  
                    }  
                }  
                else  
                {  
                    return Json(new { success = false, message = "上传的文件为空" }, JsonRequestBehavior.AllowGet);  
                }  
            }  
            else  
            {  
                return Json(new { success = false, message = "未找到上传的文件" }, JsonRequestBehavior.AllowGet);  
            }  
        }  
    }  
}
相关推荐
苦夏木禾1 小时前
在微信小程序中,同样的宽度100%,textarea和其他标签的实际宽度不一样
微信小程序·小程序
lang201509282 小时前
Spring MVC文件上传:commons-fileupload解析全流程
java·spring·mvc
岁岁种桃花儿2 小时前
Spring Boot核心注解详解:@ResponseBody深度解析与实战
spring boot·spring·mvc
计算机毕设指导63 小时前
基于微信小程序的驾校预约管理系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·intellij-idea
毕设源码-邱学长3 小时前
【开题答辩全过程】以 养老服务微信小程序为例,包含答辩的问题和答案
微信小程序·小程序
米饭同学i18 小时前
微信小程序实现动态环形进度条组件
前端·微信小程序
luffy54591 天前
微信小程序实现图片横向滑动的示例
微信小程序·小程序
棒棒的唐1 天前
使用微信小程序版Vant的upload组件上传身份证的样式自定义方案(Css魔改版)
css·微信小程序·小程序
计算机毕设指导62 天前
基于微信小程序的丽江市旅游分享系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·旅游
程序媛徐师姐2 天前
Java基于微信小程序的鲜花销售系统,附源码+文档说明
java·微信小程序·鲜花销售小程序·java鲜花销售小程序·鲜花销售微信小程序·java鲜花销售系统小程序·java鲜花销售微信小程序