使用 IntraWeb (30) - TIWAppInfo、TIWMimeTypes、TIWAppCache



TIWAppInfo   //IntraWeb 12.2.15 開始使用 TIWAppInfo 來獲取應用的相關信息, 和 IWServerController、WebApplication 的某些屬性有重復
TIWMimeTypes //IntraWeb 14.0.11 新增, 可方便處理 Http Mime Types; Mime 類型? 我的理解是: 瀏覽器接受到一個文件或數據流, 如果讓瀏覽器解析(而不是下載), 瀏覽器該按什么類型來解析呢? 所以需要指定類型.
TIWAppCache  //IntraWeb 14.0.29 新增, 可以方便處理緩存文件; 之前有類似的功能, 如: IWServerController.NewCacheFile

{三個類提供的都是 class 方法, 使用時無須實例化}


TIWAppInfo 所在單元及繼承鏈:
IW.Common.AppInfo.TIWAppInfo < TObject

主要成員:
class function GetAppFullFileName: string
class function GetAppFileName: string
class function GetAppLogFileName: string
class function GetAppPath: string
class function GetAppName: string
class function GetSystemPath: string
class function GetTempPath: string
class function GetCurrentPath: string
class function GetComputerName: string
class function GetFileInfo(const aFileName: string; aFileInfo: TFileInfo): string
class function GetAppVersion: string


測試:
uses IW.Common.AppInfo;

procedure TIWForm1.IWButton1Click(Sender: TObject);
var
  str: string;
  i: Integer;
begin
  IWMemo1.Lines.Add(TIWAppInfo.GetAppFullFileName);
  IWMemo1.Lines.Add(TIWAppInfo.GetAppFileName);
  IWMemo1.Lines.Add(TIWAppInfo.GetAppLogFileName);
  IWMemo1.Lines.Add(TIWAppInfo.GetAppPath);
  IWMemo1.Lines.Add(TIWAppInfo.GetAppName);
  IWMemo1.Lines.Add(TIWAppInfo.GetSystemPath);
  IWMemo1.Lines.Add(TIWAppInfo.GetTempPath);
  IWMemo1.Lines.Add(TIWAppInfo.GetCurrentPath);
  IWMemo1.Lines.Add(TIWAppInfo.GetComputerName);
  IWMemo1.Lines.Add(TIWAppInfo.GetAppVersion);
  IWMemo1.Lines.Add('===============');

  for i := 0 to 9 do
  begin
    str := TIWAppInfo.GetFileInfo(TIWAppInfo.GetSystemPath + 'NotePad.exe', TFileInfo(i));
    IWMemo1.Lines.Add(str);
  end;
  IWMemo1.Lines.Add('===============');
end;



TIWMimeTypes 所在單元及繼承鏈:
IWMimeTypes.TIWMimeTypes < TObject

主要成員:
class function IsStaticFile(const aFileName: string; out aMimeType: string): Boolean //
class function GetAsString(const aFileExtension: string): string //根據擴展名即可獲取類型的標准字符串很方便, 譬如 TIWMimeTypes.GetAsString('.pdf') 將返回 application/pdf
class function GetAsString(const aMimeType: TMimeType): string   //TIWMimeTypes.GetAsString(mtPDF) 結果也是 application/pdf
class procedure RegisterType(const aFileExtension: string; const aMimeTypeDesc: string; const aIsStatic: Boolean) //能自行注冊
class procedure UnregisterType(const aFileExtension: string)     //

{IWMimeTypes.TIWMimeType 枚舉的定義}
TIWMimeType = (mtUnknown, mtBinary, mtJPG, mtGIF, mtPNG, mtRSS, mtXML, mtTXT, mtICO, mtHTML, mtJavaScript, mtPDF, mtZIP, mtCSS, mtMP3, mtOGG, mtWAV, mtEXE, mtFlash, mtWMV, mtMOV, mtAVI, mtMPEG, mtXSL);

{IWMimeTypes 單元提供的常用的 Mime 類型常量}
MIME_JPG        =  'image/jpeg';                             
MIME_GIF        =  'image/gif';                              
MIME_PNG        =  'image/png';                              
MIME_RSS        =  'application/rss+xml; charset=UTF-8';     
MIME_XML        =  'text/xml; charset=UTF-8';                
MIME_XSL        =  'text/xsl; charset=UTF-8';                
MIME_TXT        =  'text/plain; charset=UTF-8';              
MIME_ICO        =  'image/x-ico';                            
MIME_JavaScript =  'application/x-javascript; charset=UTF-8';
MIME_PDF        =  'application/pdf';                        
MIME_CSS        =  'text/css; charset=UTF-8';                
MIME_MP3        =  'audio/mpeg';                             
MIME_OGG        =  'audio/ogg';                              
MIME_WAV        =  'audio/wav';                              
MIME_Flash      =  'application/x-shockwave-flash';          
MIME_WMV        =  'video/x-ms-wmv';                         
MIME_MOV        =  'video/quicktime';                        
MIME_AVI        =  'video/x-msvideo';                        
MIME_MPEG       =  'video/mpeg';                             
MIME_Binary     =  'application/octet-stream';               
MIME_HTML       =  'text/html; charset=UTF-8';

{ 更多不常用的類型可參見: http://www.iana.org/assignments/media-types/media-types.xhtml }



TIWAppCache 所在單元及繼承鏈:
IWAppCache.TIWAppCache < TObject

主要成員:
{建立緩存流; 如需特別指定第一個參數時, 不如選用下面三個函數}
class procedure NewCacheStream(aOwner: TObject; 	       //建立頁面級的緩存要指定當前窗體(一般用 Self); 建立 Session 級緩存可指定 WebApplication; 建立應用級緩存指定 nil 
                               const aContentType: string;     //Mime Type 字符串, 如: application/pdf
			       aCacheType: TCacheType; 	       //緩存期選項:ctOneTime、ctApp、ctSession、ctForm
			       out ACacheStream: TCacheStream; //輸出流
			       out aFileHRef: string	       //輸出緩存文件地址
			       )
{建立建立應用級緩存流; 參數 1 將被忽略, 其它同上}
class procedure NewAppCacheStream(aOwner: TObject; const aContentType: string; out ACacheStream: TCacheStream; out aFileHRef: string)

{建立建立 Session 級緩存流; 參數 1 將被忽略, 其它同上}
class procedure NewSessionCacheStream(aOwner: TObject; const aContentType: string; out ACacheStream: TCacheStream; out aFileHRef: string)

{建立建立頁面級緩存流; 參數 1 將被忽略, 其它同上}
class procedure NewFormCacheStream(aOwner: TObject; const aContentType: string; out ACacheStream: TCacheStream; out aFileHRef: string)

{保存流到緩存文件}
class function StreamToCacheFile(aOwner: TObject; AStream: TStream; const aContentType: string; const aCacheType: TCacheType): string

{保存圖像到緩存文件}
class function GraphicToCacheFile(aOwner: TObject; AGraphic: TGraphic; const aCacheType: TCacheType; const PreferPNG: Boolean): string
class function GraphicToCacheFile(aOwner: TObject; AGraphic: TGraphic; imgType: TIWImageOutput; const aCacheType: TCacheType): string  //TIWImageOutput = (ioGIF, ioJPEG, ioPNG)

{保存資源到緩存文件}
class function ResourceToCacheFile(aOwner: TObject; const aResourceName: string; const aContentType: string; const aCacheType: TCacheType): string

{情況緩存}
class function ClearCache(ACacheList: TStrings): Integer

{創建一個臨時文件, 位置在用戶臨時文件夾}
class function NewTempFileName: string

{添加文件到緩存}
class function AddFileToCache(aOwner: TObject; const aFileName: string; const aContentType: string; const aCacheType: TCacheType): string


測試 - 將資源中的圖片提取到緩存, 然后呈現出來:

uses IWAppCache, IWServerInternalFiles;

procedure TIWForm1.IWButton1Click(Sender: TObject);
var
  fStream: TStream;
  fPath: string;
begin
  fStream := TIWServerInternalFiles.GetResourceStream('IW_GFX_LogoIntraWeb');
  fPath := TIWAppCache.StreamToCacheFile(Self, fStream, 'image/png');
  IWImageFile1.ImageFile.Filename := fPath;
  fStream.Free;
end;


常用路徑:

{獲取代碼:-------------------------------------------------------}
uses ServerController, IW.Common.AppInfo;

procedure TIWForm1.IWAppFormCreate(Sender: TObject);
begin
  IWMemo1.Lines.Add(IWServerController.ContentPath + #9'{IWServerController.ContentPath}');
  IWMemo1.Lines.Add(IWServerController.CacheDir + #9'{IWServerController.CacheDir}');
  IWMemo1.Lines.Add(IWServerController.TemplateDir + #9'{IWServerController.TemplateDir}' + sLineBreak);

  IWMemo1.Lines.Add(WebApplication.AppUrlBase + #9'{WebApplication.AppUrlBase}');
  IWMemo1.Lines.Add(WebApplication.InternalUrlBase + #9'{WebApplication.InternalUrlBase}');
  IWMemo1.Lines.Add(WebApplication.SessionInternalUrlBase + #9'{WebApplication.SessionInternalUrlBase}');
  IWMemo1.Lines.Add(WebApplication.SessionUrlBase + #9'{WebApplication.SessionUrlBase}');
  IWMemo1.Lines.Add(WebApplication.UserCacheUrlBase + #9'{WebApplication.UserCacheUrlBase}');
  IWMemo1.Lines.Add(WebApplication.ApplicationURL + #9'{WebApplication.ApplicationURL}');
  IWMemo1.Lines.Add(WebApplication.ApplicationPath + #9'{WebApplication.ApplicationPath}');
  IWMemo1.Lines.Add(WebApplication.ReferringURL + #9'{WebApplication.ReferringURL}');
  IWMemo1.Lines.Add(WebApplication.UserCacheDir + #9'{WebApplication.UserCacheDir}' + sLineBreak);

  IWMemo1.Lines.Add(TIWAppInfo.GetAppFullFileName + #9'{TIWAppInfo.GetAppFullFileName}');
  IWMemo1.Lines.Add(TIWAppInfo.GetAppPath + #9'{TIWAppInfo.GetAppPath}');
  IWMemo1.Lines.Add(TIWAppInfo.GetAppFileName + #9'{TIWAppInfo.GetAppFileName}');
  IWMemo1.Lines.Add(TIWAppInfo.GetAppName + #9'{TIWAppInfo.GetAppName}');
  IWMemo1.Lines.Add(TIWAppInfo.GetTempPath + #9'{TIWAppInfo.GetTempPath}');
  IWMemo1.Lines.Add(TIWAppInfo.GetCurrentPath + #9'{TIWAppInfo.GetCurrentPath}');
end;

{參考結果:-------------------------------------------------------}

C:\Users\wy\Documents\RAD Studio\Projects\MyTest6\Win32\Debug\wwwroot\	{IWServerController.ContentPath}
C:\Users\wy\AppData\Local\Temp\01a3ozdw6r\	{IWServerController.CacheDir}
C:\Users\wy\Documents\RAD Studio\Projects\MyTest6\Win32\Debug\Templates\	{IWServerController.TemplateDir}

/	{WebApplication.AppUrlBase}
/$/	{WebApplication.InternalUrlBase}
/$/	{WebApplication.SessionInternalUrlBase}
/	{WebApplication.SessionUrlBase}
/$/MyApp/0pnlkje0r4hi7j19tzrq30eq0k2i/	{WebApplication.UserCacheUrlBase}
http://127.0.0.1:3126	{WebApplication.ApplicationURL}
C:\Users\wy\Documents\RAD Studio\Projects\MyTest6\Win32\Debug\	{WebApplication.ApplicationPath}
http://127.0.0.1:3126/$/start	{WebApplication.ReferringURL}
C:\Users\wy\AppData\Local\Temp\01a3ozdw6r\user\0pnlkje0r4hi7j19tzrq30eq0k2i\	{WebApplication.UserCacheDir}

C:\Users\wy\Documents\RAD Studio\Projects\MyTest6\Win32\Debug\MyTest6.exe	{TIWAppInfo.GetAppFullFileName}
C:\Users\wy\Documents\RAD Studio\Projects\MyTest6\Win32\Debug\	{TIWAppInfo.GetAppPath}
MyTest6.exe	{TIWAppInfo.GetAppFileName}
MyTest6	{TIWAppInfo.GetAppName}
C:\Users\wy\AppData\Local\Temp\	{TIWAppInfo.GetTempPath}
C:\Users\wy\Documents\RAD Studio\Projects\MyTest6\Win32\Debug\	{TIWAppInfo.GetCurrentPath}



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM