protected void DeleteSysFile(int id, string file_kind, string file_name, ImageUpScope im, string category1, string category2)
        {
            string tpl_FolderPath = Server.MapPath(string.Format(upload_path_tpl_s, category1, category2, id, file_kind));

            string handle_delete_file = tpl_FolderPath + "/" + file_name;

            if (System.IO.File.Exists(handle_delete_file))
            {
                System.IO.File.Delete(handle_delete_file);
            }
            #region Delete Run
            if (Directory.Exists(tpl_FolderPath))
            {
                var folders = Directory.GetDirectories(tpl_FolderPath);
                foreach (var folder in folders)
                {
                    string herefile = folder + "\\" + file_name;
                    if (System.IO.File.Exists(herefile))
                    {
                        System.IO.File.Delete(herefile);
                    }
                }
            }
            #endregion

            #region Handle Json Info
            string file_json_web_path    = string.Format(upload_path_tpl_s, category1, category2, id, file_kind);
            string file_json_server_path = Server.MapPath(file_json_web_path) + "\\file.json";

            IList <JsonFileInfo> get_file_json_object = null;
            if (System.IO.File.Exists(file_json_server_path))
            {
                var read_json = System.IO.File.ReadAllText(file_json_server_path);
                get_file_json_object = JsonConvert.DeserializeObject <IList <JsonFileInfo> >(read_json);
                var get_file_object = get_file_json_object.Where(x => x.fileName == file_name).FirstOrDefault();
                if (get_file_object != null)
                {
                    get_file_json_object.Remove(get_file_object);
                    int i = 1;
                    foreach (var file_object in get_file_json_object)
                    {
                        file_object.sort = i;
                        i++;
                    }
                    var json_string = JsonConvert.SerializeObject(get_file_json_object);
                    System.IO.File.WriteAllText(file_json_server_path, json_string, Encoding.UTF8);
                }
            }
            #endregion
        }
        protected void DeleteSysFile(int Id, string fileskind, string filename, ImageUpScope im)
        {
            string SystemDelSysIdKind = "~/_Upload/{0}/{1}/{2}/{3}";
            string tpl_FolderPath     = Server.MapPath(String.Format(SystemDelSysIdKind, getArea, getController, Id, fileskind));

            #region Delete Run
            if (Directory.Exists(tpl_FolderPath))
            {
                var folders = Directory.GetDirectories(tpl_FolderPath);
                foreach (var folder in folders)
                {
                    String herefile = folder + "\\" + filename;
                    if (System.IO.File.Exists(herefile))
                    {
                        System.IO.File.Delete(herefile);
                    }
                }
            }
            #endregion
        }
        protected void handleImageSave(string file_name, int id, ImageUpScope fp, string file_kind, string category1, string category2)
        {
            BinaryReader binary_read = null;
            string       file_ext    = System.IO.Path.GetExtension(file_name); //取得副檔名

            string[] ie_older_ver = new string[] { "6.0", "7.0", "8.0", "9.0" };

            if (Request.Browser.Browser == "IE" && ie_older_ver.Any(x => x == Request.Browser.Version))
            {
                #region IE file stream handle
                HttpPostedFile get_post_file = System.Web.HttpContext.Current.Request.Files[0];
                if (!get_post_file.FileName.Equals(""))
                {
                    binary_read = new BinaryReader(get_post_file.InputStream);
                }
                #endregion
            }
            else
            {
                binary_read = new BinaryReader(Request.InputStream);
            }

            byte[] upload_file = binary_read.ReadBytes(System.Convert.ToInt32(binary_read.BaseStream.Length));

            string web_path_org    = string.Format(upload_path_tpl_o, category1, category2, id, file_kind, "origin");
            string server_path_org = Server.MapPath(web_path_org);

            #region 檔案上傳前檢查
            if (fp.limitSize > 0) //檔案大小檢查
            {
                if (binary_read.BaseStream.Length > fp.limitSize)
                {
                    throw new LogicError("Log_Err_FileSizeOver");
                }
            }

            if (fp.limitCount > 0 && Directory.Exists(server_path_org))
            {
                string[] Files = Directory.GetFiles(server_path_org);
                if (Files.Count() >= fp.limitCount) //還沒存檔,因此Selet到等於的數量,再加上現在要存的檔案即算超過
                {
                    throw new LogicError("Log_Err_FileCountOver");
                }
            }

            if (fp.allowExtType != null)
            {
                if (!fp.allowExtType.Contains(file_ext.ToLower()))
                {
                    throw new LogicError("Log_Err_AllowFileType");
                }
            }

            if (fp.limitExtType != null)
            {
                if (fp.limitExtType.Contains(file_ext))
                {
                    throw new LogicError("Log_Err_LimitedFileType");
                }
            }
            #endregion
            #region 存檔區

            if (fp.keepOrigin)
            {
                //原始檔
                if (!System.IO.Directory.Exists(server_path_org))
                {
                    System.IO.Directory.CreateDirectory(server_path_org);
                }

                FileStream   file_stream  = new FileStream(server_path_org + "\\" + file_name, FileMode.Create);
                BinaryWriter binary_write = new BinaryWriter(file_stream);
                binary_write.Write(upload_file);

                file_stream.Close();
                binary_write.Close();
            }

            //後台管理的ICON小圖
            string web_path_icon    = string.Format(upload_path_tpl_o, category1, category2, id, file_kind, "icon");
            string server_path_icon = Server.MapPath(web_path_icon);
            if (!System.IO.Directory.Exists(server_path_icon))
            {
                System.IO.Directory.CreateDirectory(server_path_icon);
            }
            MemoryStream smr = resizeImage(upload_file, 0, 90);
            System.IO.File.WriteAllBytes(server_path_icon + "\\" + Path.GetFileName(file_name), smr.ToArray());
            smr.Dispose();

            //依據參數進行裁圖
            if (fp.Parm.Count() > 0)
            {
                string web_path_parm    = string.Format(upload_path_tpl_s, category1, category2, id, file_kind);
                string server_path_parm = Server.MapPath(web_path_parm);
                foreach (ImageSizeParm imSize in fp.Parm)
                {
                    MemoryStream sm = resizeImage(upload_file, imSize.width, imSize.heigh);
                    System.IO.File.WriteAllBytes(server_path_parm + "\\" + Path.GetFileName(file_name), sm.ToArray());
                    sm.Dispose();
                }
            }
            #endregion

            #region Handle Json Info
            string file_json_web_path    = string.Format(upload_path_tpl_s, category1, category2, id, file_kind);
            string file_json_server_path = Server.MapPath(file_json_web_path) + "\\file.json";

            IList <JsonFileInfo> f = null;
            int sort = 0;
            if (System.IO.File.Exists(file_json_server_path))
            {
                var read_json = System.IO.File.ReadAllText(file_json_server_path);
                f = JsonConvert.DeserializeObject <IList <JsonFileInfo> >(read_json);
                if (f.Any(x => x.fileName == file_name))
                {
                    return;
                }

                sort = f.Count + 1;
            }
            else
            {
                f    = new List <JsonFileInfo>();
                sort = 1;
            }



            f.Add(new JsonFileInfo()
            {
                fileName = file_name,
                sort     = sort
            });

            var json_string = JsonConvert.SerializeObject(f);
            System.IO.File.WriteAllText(file_json_server_path, json_string, Encoding.UTF8);
            #endregion
        }
Exemple #4
0
        protected void HandImageSave(String FileName, int Id, ImageUpScope fp, String FilesKind)
        {
            Stream       upFileStream = Request.InputStream;
            BinaryReader BinRead      = new BinaryReader(upFileStream);
            String       FileExt      = System.IO.Path.GetExtension(FileName);

            #region IE file stream handle

            String[] IEOlderVer = new string[] { "6.0", "7.0", "8.0", "9.0" };
            System.Web.HttpPostedFile GetPostFile = null;
            if (Request.Browser.Browser == "IE" && IEOlderVer.Any(x => x == Request.Browser.Version))
            {
                System.Web.HttpFileCollection collectFiles = System.Web.HttpContext.Current.Request.Files;
                GetPostFile = collectFiles[0];
                if (!GetPostFile.FileName.Equals(""))
                {
                    //GetFileName = System.IO.Path.GetFileName(GetPostFile.FileName);
                    BinRead = new BinaryReader(GetPostFile.InputStream);
                }
            }

            Byte[] fileContents = { };
            //const int bufferSize = 1024 * 16; //set 16K buffer

            while (BinRead.BaseStream.Position < BinRead.BaseStream.Length - 1)
            {
                //Byte[] buffer = new Byte[bufferSize];
                Byte[] buffer  = new Byte[BinRead.BaseStream.Length - 1];
                int    ReadLen = BinRead.Read(buffer, 0, buffer.Length);
                Byte[] dummy   = fileContents.Concat(buffer).ToArray();
                fileContents = dummy;
                dummy        = null;
            }
            #endregion

            String tpl_Org_FolderPath = String.Format(SystemUpFilePathTpl, GetArea, GetController, Id, FilesKind, "OriginFile");
            String Org_Path           = Server.MapPath(tpl_Org_FolderPath);

            #region 檔案上傳前檢查
            if (fp.LimitSize > 0)
            {
                //if (GetPostFile.InputStream.Length > fp.LimitSize)
                if (BinRead.BaseStream.Length > fp.LimitSize)
                {
                    throw new LogicError("Log_Err_FileSizeOver");
                }
            }

            if (fp.LimitCount > 0 && Directory.Exists(Org_Path))
            {
                String[] Files = Directory.GetFiles(Org_Path);
                if (Files.Count() >= fp.LimitCount) //還沒存檔,因此Selet到等於的數量,再加上現在要存的檔案即算超過
                {
                    throw new LogicError("Log_Err_FileCountOver");
                }
            }

            if (fp.AllowExtType != null)
            {
                if (!fp.AllowExtType.Contains(FileExt.ToLower()))
                {
                    throw new LogicError("Log_Err_AllowFileType");
                }
            }

            if (fp.LimitExtType != null)
            {
                if (fp.LimitExtType.Contains(FileExt))
                {
                    throw new LogicError("Log_Err_LimitedFileType");
                }
            }
            #endregion

            #region 存檔區

            if (fp.KeepOriginImage)
            {
                //原始檔
                //tpl_Org_FolderPath = String.Format(SystemUpFilePathTpl, GetArea, GetController, Id, FilesKind, "OriginFile");
                Org_Path = Server.MapPath(tpl_Org_FolderPath);
                if (!System.IO.Directory.Exists(Org_Path))
                {
                    System.IO.Directory.CreateDirectory(Org_Path);
                }

                FileStream   writeStream = new FileStream(Org_Path + "\\" + FileName, FileMode.Create);
                BinaryWriter BinWrite    = new BinaryWriter(writeStream);
                BinWrite.Write(fileContents);

                upFileStream.Close();
                upFileStream.Dispose();
                writeStream.Close();
                BinWrite.Close();
                writeStream.Dispose();
                BinWrite.Dispose();
                //FileName.SaveAs(Org_Path + "\\" + FileName.FileName.GetFileName());
            }

            //後台管理的代表小圖
            String tpl_Rep_FolderPath = String.Format(SystemUpFilePathTpl, GetArea, GetController, Id, FilesKind, "RepresentICON");
            String Rep_Path           = Server.MapPath(tpl_Rep_FolderPath);
            if (!System.IO.Directory.Exists(Rep_Path))
            {
                System.IO.Directory.CreateDirectory(Rep_Path);
            }
            MemoryStream smr = UpFileReSizeImage(fileContents, 0, 90);
            System.IO.File.WriteAllBytes(Rep_Path + "\\" + FileName.GetFileName(), smr.ToArray());
            smr.Dispose();

            if (fp.Parm.Count() > 0)
            {
                foreach (ImageSizeParm imSize in fp.Parm)
                {
                    tpl_Rep_FolderPath = String.Format(SystemUpFilePathTpl, GetArea, GetController, Id, FilesKind, "s_" + imSize.SizeFolder);
                    Rep_Path           = Server.MapPath(tpl_Rep_FolderPath);
                    if (!System.IO.Directory.Exists(Rep_Path))
                    {
                        System.IO.Directory.CreateDirectory(Rep_Path);
                    }
                    MemoryStream sm = UpFileReSizeImage(fileContents, imSize.width, imSize.heigh);
                    System.IO.File.WriteAllBytes(Rep_Path + "\\" + FileName.GetFileName(), sm.ToArray());
                    sm.Dispose();
                }
            }
            #endregion
        }