首页 > Web开发 > 详细

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

时间:2017-11-10 19:24:33      阅读:441      评论:0      收藏:0      [点我收藏+]

前端:

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!