微信小程序上传图片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);  
            }  
        }  
    }  
}
相关推荐
萧寂1731 小时前
微信小程序自定义轮播图指示点样式
微信小程序·小程序
SuperherRo10 小时前
自动化反编译微信小程序工具-e0e1-wx
微信小程序·自动化·hook·反编译
R_yy10 小时前
微信小程序开发——视频播放实现(本地视频或者云端视频均可)
前端·微信小程序·小程序
hadage23311 小时前
--- spring MVC ---
java·spring·mvc
京河小蚁13 小时前
解决Moodo调节心情模块-大声喊出来无法测量出音频分贝
微信小程序·音视频·ai编程·cursor
嘵奇14 小时前
Spring MVC框架六:Ajax技术
mvc
蒜香拿铁15 小时前
小程序参数获取方式
前端·微信小程序
Rverdoser16 小时前
在 Centos7 上部署 ASP.NET 8.0 + YOLOv11 的踩坑实录
后端·yolo·asp.net
V+zmm1013417 小时前
智慧物流小程序(论文源码调试讲解)
java·数据库·微信小程序·小程序·毕业设计
霍格沃兹测试开发学社测试人社区18 小时前
性能测试丨微信小程序性能优化指南
性能优化·微信小程序·小程序