微信小程序上传图片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. TCP协议是安全的吗?

    2024-03-17 06:34:05       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-17 06:34:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-17 06:34:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-17 06:34:05       18 阅读

热门阅读

  1. Spring核心方法:Refresh全解(WebMVC如何装配、关联)

    2024-03-17 06:34:05       19 阅读
  2. 阿里提前批(阿里云)一面30min

    2024-03-17 06:34:05       21 阅读
  3. MATLAB中的矩阵和数组,它们之间有什么区别?

    2024-03-17 06:34:05       20 阅读
  4. C语言每日一题—判断是否为魔方矩阵

    2024-03-17 06:34:05       17 阅读
  5. C++ 基础组件(1)定时器

    2024-03-17 06:34:05       16 阅读
  6. 【C++】每日一题 228 汇总区间

    2024-03-17 06:34:05       14 阅读
  7. Spring之底层架构核心概念解析

    2024-03-17 06:34:05       18 阅读
  8. c# 修改数据集

    2024-03-17 06:34:05       19 阅读
  9. 合作测试开发日志1

    2024-03-17 06:34:05       14 阅读
  10. go的fasthttp学习

    2024-03-17 06:34:05       23 阅读
  11. uniapp 分包

    2024-03-17 06:34:05       18 阅读
  12. 华为交换机端口类型:Access、Trunk、Hybrid

    2024-03-17 06:34:05       19 阅读
  13. day01.蓝桥杯

    2024-03-17 06:34:05       16 阅读