• MVC文件的上传、删除


    public ActionResult FileUpload()
            {
                Users users = new Users();
                users = (Users)Session["Users"];
                HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;//获取上传的文件
                if (files.Count == 0)//判断文件是否为空
                    return Json("Faild", JsonRequestBehavior.AllowGet);
                //MD5 md5Hasher = new MD5CryptoServiceProvider();
                /*计算指定Stream对象的哈希值*/
                //byte[] arrbytHashValue = md5Hasher.ComputeHash(files[0].InputStream);
                /*由以连字符分隔的十六进制对构成的String,其中每一对表示value中对应的元素;例如“F-2C-4A”*/
                //string strHashData = BitConverter.ToString(arrbytHashValue).Replace("-", "");
                var FileName = files[0].FileName.Substring(0,files[0].FileName.IndexOf('.'));//获取文件名称
                var FileEextension = Path.GetExtension(files[0].FileName);//获取文件后缀名
                var uploadDate = DateTime.Now/*.ToString("yyyyMMddHHmm")*/;//获取文件上传时间
                var addres = "";
                //判断文件类型存入不同文件夹
                if(FileEextension == ".doc"||FileEextension == ".docx")
                {
                    addres = "DOC";
                }
                if (FileEextension == ".xls" || FileEextension == ".xlsx")
                {
                    addres = "Excel";
                }
                if (FileEextension == ".ppt" || FileEextension == ".pptx")
                {
                    addres = "PPT";
                }
                if (FileEextension == ".pdf")
                {
                    addres = "PDF";
                }
                string virtualPath = string.Format("/ResourceFile/{0}/{1}{2}", addres, FileName, FileEextension);//当前文件相对路径
                string fullFileName = Server.MapPath(virtualPath);//文件的绝对路径
                //创建文件夹,保存文件
                string path = Path.GetDirectoryName(fullFileName);
                Directory.CreateDirectory(path);//判断是否有这个路径,没有就创建
               
                string fileName = files[0].FileName.Substring(files[0].FileName.LastIndexOf("\") + 1, files[0].FileName.Length - files[0].FileName.LastIndexOf("\") - 1);
                var fileSize = GetFileSize(files[0].ContentLength);
                if (fileSize == null)
                {
                    return null;
                }
                if (!System.IO.File.Exists(fullFileName))
                {
                    files[0].SaveAs(fullFileName);
                }
                //添加信息
                Resource resource = new Resource();
                resource.UsersId = users.Id;
                resource.Title = FileName;
                resource.Extensions = FileEextension.Substring(1,FileEextension.Length-1);
                resource.UploadTime = uploadDate;
                resource.FileSize =Convert.ToInt32(fileSize.Substring(0,fileSize.Length-2));
                resource.UploadStage = 1;
                int i = _IExplorerRespository.Add(resource);
                if (i > 0)
                {
                    return Json(new { FileName = fileName, FilePath = virtualPath, FileSize = fileSize }, "text/html", JsonRequestBehavior.AllowGet);
                }
                else
                {
                    FileInfo f = new FileInfo(FileName+ FileEextension);
                    DirectoryInfo dir = f.Directory;
                    foreach (FileInfo item in dir.GetFiles())
                    {
                        try
                        {
                            //删除文件
                            if(System.IO.File.Exists(fullFileName))
                            {
                                FileInfo fi = new FileInfo(fullFileName);
                                if(fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                                {
                                    fi.Attributes = FileAttributes.Normal;
                                }
                                System.IO.File.Delete(fullFileName);
                            }
                        }
                        catch
                        {
                           
                        }
                    }
                }
            }

    文件转载加改编

  • 相关阅读:
    【Python之路】第三篇--Python基本数据类型
    【Python之路】第二篇--初识Python
    【Python之路】第一篇--Linux基础命令
    noip模拟测试7
    noip模拟测试6
    动态添加select的option [转载]
    javaweb报错:java.lang.NumberFormatException: null
    Javascript获取select的选中值和选中文本(转载)
    动态生成select框内容
    IO(Input&Output)流の介绍
  • 原文地址:https://www.cnblogs.com/daran/p/10535115.html
Copyright © 2020-2023  润新知