码迷,mamicode.com
首页 > Web开发 > 详细

上传图片

时间:2019-04-01 21:28:54      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:check   process   ted   tab   string   substring   substr   collect   tostring   

 
 
  /// <summary>
        /// 前端Ajax
        /// </summary>
        /// <returns></returns>
 function Upload()
        {
            var HobbyName = "";
            $("input[name=Hobby]:checked").each(function () {
                HobbyName += $(this).val() + ‘,‘;
            })
            var formdata = new FormData($("#file")[0]);
            formdata.append("form1", 1);
            formdata.append(‘Name‘, $("#txt_Name").val());
            formdata.append(‘Sex‘, $("input[name=Sex]:checked").val());
            formdata.append(‘Hobby‘, HobbyName.substring(0, HobbyName.length - 1));
            formdata.append(‘Grade‘, $("#sele_Grade option:selected").val());
            formdata.append("Photo", $("#file").get(0).files[0]);
            formdata.append("FuWenben", $("#txt_FuWenben").val());
            $.ajax({
                url: ‘/Student/Upload‘,
                data: formdata,
                type: ‘post‘,
                dataType: ‘json‘,
                processData: false,
                contentType: false,
                success: function (res) {
                    if (res > 0) {
                        alert("添加成功!");
                        window.location.href = "/Student/Show";
                    }
                    else {
                        alert("恭喜失败!");
                    }
                }
            })
        }
 
       /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public int Upload()
        {
            string Hobby = "";
            Student student = new Student();
            HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            foreach (var item in System.Web.HttpContext.Current.Request["Hobby"].Split(‘,‘))
            {
                 Hobby += item;
            }
            if (files.Count > 0)
            {
                HttpPostedFile fpostfile = files[0];
                string text = Path.GetExtension(fpostfile.FileName);
                fpostfile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("/Image/") + fpostfile.FileName);
                student.Name = System.Web.HttpContext.Current.Request["Name"];
                student.Sex = System.Web.HttpContext.Current.Request["Sex"];
                student.Hobby = Hobby;
                student.Grade = System.Web.HttpContext.Current.Request["Grade"];
                student.Photo = fpostfile.FileName;
                student.FuWenben = System.Web.HttpContext.Current.Request["FuWenben"];
                string fullpath = Server.MapPath("/Image/"+fpostfile.FileName);
                string path = Path.GetDirectoryName(fullpath);
                files[0].SaveAs(fullpath);
                if(dal.Add(student)>0)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }
 
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public int Del(int id)
        {
            DataTable str = dal.GetDel(id);
            string path = "C:/Users/Shinelon/Desktop/Unit1/WebApplication2/Image/" + str.Rows[0].ItemArray[0].ToString();
            FileAttributes attr = System.IO.File.GetAttributes(path);
            if(attr == FileAttributes.Directory)
            {
                Directory.Delete(path,true);
                int i = dal.Delete(id);
                return i;
            }
            else
            {
                System.IO.File.Delete(path);
                int i = dal.Delete(id);
                return i;
            }
        }

上传图片

标签:check   process   ted   tab   string   substring   substr   collect   tostring   

原文地址:https://www.cnblogs.com/daran/p/10638926.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!