Exemplo n.º 1
0
        public ActionResult Index(string newLink, string searchName)
        {
            BrowseFileModel model       = new BrowseFileModel();
            var             path        = "";
            var             virtualPath = "";

            if (newLink == null)
            {
                path = Server.MapPath("~/生產資訊/");
            }
            else
            {
                var startIndex = newLink.IndexOf("生");
                var leng       = newLink.Length;
                var finalIndex = newLink.Length - 1;
                virtualPath = newLink.Substring(startIndex);
                newLink     = "~/" + newLink.Substring(startIndex);
                path        = Server.MapPath(newLink);
            }
            var dir = new DirectoryInfo(path);

            ViewBag.fileName   = dir.Name;
            ViewBag.parentName = dir.Parent;
            var attribute = dir.Attributes;

            if (attribute.ToString() == "Directory")
            {
                var files = dir.EnumerateFileSystemInfos().Select(f => f).OrderBy(f => f.Attributes);
                if (searchName != null)
                {
                    files = dir.EnumerateFileSystemInfos().Select(f => f).Where(f => f.Name.ToUpper().Contains(searchName.ToUpper())).OrderBy(f => f.Attributes);
                }
                model.DirectoryInfo = dir;
                model.FileInfo      = files;
                return(View(model));
            }
            else if (dir.Extension.ToString() == ".jpg" || dir.Extension.ToString() == ".png")
            {
                return(RedirectToAction("Preview", "Home", new { path = newLink }));
            }
            else
            {
                byte[] fileByte = System.IO.File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + virtualPath);
                return(File(fileByte, System.Net.Mime.MediaTypeNames.Application.Octet, dir.Name));
            }
        }
Exemplo n.º 2
0
        public ActionResult ViewFiles(string newLink, string searchName, string folderName)
        {
            BrowseFileModel model       = new BrowseFileModel();
            var             path        = "";
            var             virtualPath = "";

            if (newLink == null)
            {
                path = Server.MapPath("~/生產資訊/");
            }
            else
            {
                var startIndex = newLink.IndexOf("生");
                var leng       = newLink.Length;
                var finalIndex = newLink.Length - 1;
                virtualPath = newLink.Substring(startIndex);
                newLink     = "~/" + newLink.Substring(startIndex);
                path        = Server.MapPath(newLink);
            }
            var dir = new DirectoryInfo(path);

            ViewBag.fileName   = dir.Name;
            ViewBag.parentName = dir.Parent;
            var attribute = dir.Attributes;

            if (folderName != null && folderName.Length > 0)
            {
                var newFolder = Server.MapPath(newLink + "\\" + folderName);
                if (!Directory.Exists(newFolder))
                {
                    Directory.CreateDirectory(newFolder);
                }
                else
                {
                    ViewBag.duplicateFolder = true;
                }
            }
            else if (folderName != null && folderName.Length == 0)
            {
                ViewBag.needFolderName = true;
            }
            if (attribute.ToString() == "Directory")
            {
                var files = dir.EnumerateFileSystemInfos().Select(f => f).OrderBy(f => f.Attributes);
                if (searchName != null && searchName.Length > 0)
                {
                    files            = dir.EnumerateFileSystemInfos().Select(f => f).Where(f => f.Name.ToUpper().Contains(searchName.ToUpper())).OrderBy(f => f.Attributes);
                    ViewBag.isSearch = true;
                }
                else
                {
                    ViewBag.isSearch = false;
                }
                model.DirectoryInfo = dir;
                model.FileInfo      = files;
                return(View(model));
            }
            else if (dir.Extension.ToString() == ".pdf")
            {
                return(File(newLink, "application.pdf"));
            }
            else
            {
                byte[] fileByte = System.IO.File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + virtualPath);
                return(File(fileByte, System.Net.Mime.MediaTypeNames.Application.Octet, dir.Name));
            }
        }