上传图片

Posted daran

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了上传图片相关的知识,希望对你有一定的参考价值。

 
 
  /// <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;
            }
        }

以上是关于上传图片的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET上传控件上传图片

我本地的图片怎么上传上去?就是图片要弄地址?

PHP Fckeditor 上传图片的问题

php 在多个上传图片时 怎么获取 要上传的图片宽高

我的论坛上传图片为啥压缩太厉害?

ThinkPHP 上传图片压缩原图片?