protected void handleFileSave(string file_name, int id, FilesUpScope fp, string file_kind, string category1, string category2) { Stream file_stream = Request.InputStream; BinaryReader binary_read = new BinaryReader(file_stream); string file_ext = System.IO.Path.GetExtension(file_name); #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("")) { binary_read = new BinaryReader(GetPostFile.InputStream); } } Byte[] fileContents = { }; while (binary_read.BaseStream.Position < binary_read.BaseStream.Length - 1) { Byte[] buffer = new Byte[binary_read.BaseStream.Length - 1]; int read_line = binary_read.Read(buffer, 0, buffer.Length); Byte[] dummy = fileContents.Concat(buffer).ToArray(); fileContents = dummy; dummy = null; } #endregion string web_path_org = string.Format(upload_path_tpl_s, category1, category2, id, file_kind); 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) { 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 (!System.IO.Directory.Exists(server_path_org)) { System.IO.Directory.CreateDirectory(server_path_org); } FileStream write_stream = new FileStream(server_path_org + "\\" + file_name, FileMode.Create); BinaryWriter binary_write = new BinaryWriter(write_stream); binary_write.Write(fileContents); file_stream.Close(); write_stream.Close(); binary_write.Close(); #endregion }
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 }