C# MVC Web项目和Layui多图上传

本文介绍了如何在C# MVC Web项目中结合Layui实现多图上传功能,重点提示了图片大小限制为3.7MB。
摘要由CSDN通过智能技术生成

⚠️注意:图片不可大于3707.6kb

效果图:
在这里插入图片描述

public class UploadController : Controller
    {
   

        public ActionResult upload()
        {
   
            return View();
        }

        #region 文件处理


        /// <summary>
        /// 上传附件 到临时文件夹
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        ///

        [HttpPost]
        public ActionResult UpAttachment()
        {
   
            HttpPostedFileBase httpPostedFileBase = Request.Files[0];//获取二进制图片文件流
            if (httpPostedFileBase != null)
            {
   
                try
                {
   
                    ControllerContext.HttpContext.Request.ContentEncoding = Encoding.GetEncoding("UTF-8");
                    ControllerContext.HttpContext.Response.Charset = "UTF-8";

                    string fileName = Path.GetFileName(httpPostedFileBase.FileName);//原始文件名称
                    string fileExtension = Path.GetExtension(fileName);//文件扩展名

                    byte[] fileData = ReadFileBytes(httpPostedFileBase);//文件流转化为二进制字节

                    string result = SaveFile(fileExtension, fileData);//文件保存
                    if (string.IsNullOrEmpty(result))
                    {
   
                        return Json(new {
    isSuccess = false, path = "", errorMsg = "上传文件失败" });
                    }

                    return Json(new {
    isSuccess = true, path = result });
                }
                catch (Exc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值