C#结合js 上传文件和删除文件,技术点有:asp.net mvc ,nhibernate,ajax等

之前做项目的时候要用到上传文件的功能,现在我总结一下,上传文件和删除文件的代码,在以后的使用的过程中也更方便查找。

[HttpPost]
        public ActionResult EditUser()
        {
            var userDal = new UserDal();
            const string savePath = "/Images/Avatar/";
            const string saveUrl = "/Images/Avatar/";
            const string fileTypes = "gif,jpg,jpeg,png,bmp";
            const int maxSize = 1000000;

            Hashtable hash;

            HttpPostedFile file = System.Web.HttpContext.Current.Request.Files["upload"];
            var id = System.Web.HttpContext.Current.Request.Params["id"];
            var posttitle = System.Web.HttpContext.Current.Request.Params["posttitle"];
            if (file == null)
            {
                hash = new Hashtable();
                hash["success"] = false;
                hash["msg"] = "请选择上传文件";
                return Json(hash, "text/html;charset=UTF-8");
            }

            string dirPath = System.Web.HttpContext.Current.Server.MapPath(savePath);
            if (!Directory.Exists(dirPath))
            {
                hash = new Hashtable();
                hash["success"] = false;
                hash["msg"] = "上传目录不存在";
                return Json(hash, "text/html;charset=UTF-8");
            }

            string fileName = file.FileName;
            string fileExt = Path.GetExtension(fileName).ToLower();

            ArrayList fileTypeList = ArrayList.Adapter(fileTypes.Split(','));

            if (file.InputStream == null || file.InputStream.Length > maxSize)
            {
                hash = new Hashtable();
                hash["success"] = false;
                hash["msg"] = "上传文件大小超过限制";
                return Json(hash, "text/html;charset=UTF-8");
            }

            if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(fileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1)
            {
                hash = new Hashtable();
                hash["success"] = false;
                hash["msg"] = "上传文件扩展名是不允许的扩展名";
                return Json(hash, "text/html;charset=UTF-8");
            }

            string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
            string filePath = dirPath + newFileName;
            file.SaveAs(filePath);
            string fileUrl = saveUrl + newFileName;

            hash = new Hashtable();
            try
            {
                var user = userDal.GetByUser(id);
                var imgurl = user.Avatar;
                var pathall = _userimgpath + imgurl.Replace("/","\\");
                if (System.IO.File.Exists(pathall))
                {
                    System.IO.File.Delete(pathall);
                }
                userDal.Update(new User()
                {
                    Id = id,
                    PostTitle = posttitle,
                    Avatar = fileUrl
                });
                hash["success"] = true;
                hash["msg"] = "上传成功";
                return Json(hash, "text/html;charset=UTF-8");
            }
            catch (Exception)
            {
                hash["success"] = false;
                hash["msg"] = "上传失败";
                return Json(hash, "text/html;charset=UTF-8");
            }
        }

总结一下:

将前台上传的文件通过System.Web.HttpContext.Current.Request.Files[]方法获取到然后根据已知的路径保存到文件夹中,如果已经存在了这个文件,就将它从文件夹中删除,最后将成功或失败的数据通过hashtable转换成json传递到前台,一个C#的文件上传和删除文件就这样完成了。