Exemplo n.º 1
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
        }
Exemplo n.º 2
0
        protected void HandFineSave(String FileName, int Id, FilesUpScope fp, String FilesKind, Boolean pdfConvertImage)
        {
            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; //set 1k buffer

            while (BinRead.BaseStream.Position < BinRead.BaseStream.Length - 1)
            {
                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 (!System.IO.Directory.Exists(Org_Path))
            {
                System.IO.Directory.CreateDirectory(Org_Path);
            }

            //LogWrite.Write("Save File Start"+ Org_Path + "\\" + FileName);

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

            upFileStream.Close();
            upFileStream.Dispose();
            writeStream.Close();
            BinWrite.Close();
            writeStream.Dispose();
            BinWrite.Dispose();

            //LogWrite.Write("Save File End"+ Org_Path + "\\" + FileName);
            #endregion

            #region PDF轉圖檔
            if (pdfConvertImage)
            {
                FileInfo fi = new FileInfo(Org_Path + "\\" + FileName);
                if (fi.Extension == ".pdf")
                {
                    System.Diagnostics.Process proc = new System.Diagnostics.Process();
                    proc.StartInfo.UseShellExecute        = false;
                    proc.StartInfo.FileName               = @"C:\Program Files\Boxoft PDF to JPG (freeware)\pdftojpg.exe";
                    proc.StartInfo.Arguments              = Org_Path + "\\" + FileName + " " + Org_Path;
                    proc.StartInfo.RedirectStandardInput  = true;
                    proc.StartInfo.RedirectStandardOutput = true;
                    proc.StartInfo.RedirectStandardError  = true;
                    proc.Start();
                    proc.WaitForExit();
                    proc.Close();
                    proc.Dispose();
                }
            }
            #endregion
        }