分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

ASP.NET图片上传(配合jquery.from.js 插件)

发布时间:2023-09-06 01:24责任编辑:沈小雨关键词:.NETjs

前端:

js:
        function AjaxKouBeiShopEdit() {

            var options = {
                dataType: "json",
                success: function (data) {
                    alert(data);
                 
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(textStatus);
                    console.log(errorThrown);
                }
            };
          
            $("#ShopForm").ajaxSubmit(options);

        }

html:

 <form class="form-horizontal" id="ShopForm" action="你的控制器/AjaxKouBeiShopEdit" method="post" enctype="multipart/form-data">

  <input type="file"  name="ProductImg" class="fileData"    />

  <input type="file"  name="ProductImg" class="fileData"    />

</form>

后端:
        /// <summary>
        /// 口碑门店-添加
        /// </summary>
        /// <param name="upImg"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public string AjaxKouBeiShopEdit(KouBeiShopModel entity)
        {
            StringBuilder sqlStr = new StringBuilder();
            TimeSpan timeSpan = (DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)));
            Random random = new Random();
            JObject resultJsonerr = new JObject();
            //修改操作
                #region  添加文件
                string filePhysicalPath = @"D:/yyyy/";
                string MainImgName = "";

                //详细图片路径
                if (Request.Files.Count != 0)
                {
                    MainImgName = filePhysicalPath + MainImgName;
                    try
                    {
                        for (int i = 0; i < Request.Files.Count;i++ )
                        {
                            HttpPostedFileBase uploadFile = Request.Files[i] as HttpPostedFileBase;
                            if (uploadFile != null && uploadFile.ContentLength > 0)
                            {
                              //  if (i==0)//首图
                             //   {
                                //html 上至下加载,file  input
                             //   }
         //随机名称
                                long t2 = (long)timeSpan.TotalSeconds;
                                int n2 = random.Next(10000, 99999);
                                string extension = Path.GetExtension(uploadFile.FileName);
                                string ImgListName = t2 + "" + n2 + extension;
                                var path = Path.Combine(filePhysicalPath, ImgListName);
                                uploadFile.SaveAs(path);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        resultJsonerr.Add("State", -1);
                        return "添加失败";
                    }
                }

  
            return "添加成功";

        }

ASP.NET图片上传(配合jquery.from.js 插件)

原文地址:http://www.cnblogs.com/j2ee-web-01/p/7816002.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved