使用 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}

你可能感兴趣的:(使用 IntraWeb (30) - TIWAppInfo、TIWMimeTypes、TIWAppCache)