微信小程序上传图片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);  
            }  
        }  
    }  
}
相关推荐
我叫逢7 小时前
一键去水印实战已上线!心得~
微信小程序·php·去水印
jumu2029 小时前
C#运控框架:支持GTS400/800的固高运动控制源码
asp.net
qq_124987075310 小时前
基于微信小程序的电子元器件商城(源码+论文+部署+安装)
java·spring boot·spring·微信小程序·小程序·毕业设计
壹立科技16 小时前
商超到家即时服务:软件基础功能打通“线上线下”关键链路
微信小程序·软件需求·外卖跑腿平台·外卖跑腿系统·商超配送
计算机毕设指导618 小时前
基于微信小程序+django连锁火锅智慧餐饮管理系统【源码文末联系】
java·后端·python·mysql·微信小程序·小程序·django
风月歌18 小时前
php医院预约挂号系统小程序源代码(源码+文档+数据库)
数据库·微信小程序·小程序·毕业设计·php·源码
稀饭过霍18 小时前
【.NET 10.0】使用FluentValidation
c#·mvc·.net
qq_124987075319 小时前
基于微信小程序的校园资讯共享平台的设计与实现(源码+论文+部署+安装)
spring boot·后端·微信小程序·小程序·毕业设计
计算机毕设指导619 小时前
基于微信小程序的派出所业务管理系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·uniapp
Aevget19 小时前
DevExpress JS & ASP.NET Core v25.1新版亮点 - 新增AI文本编辑功能
javascript·人工智能·asp.net·界面控件·devexpress·ui开发