在报表中显示指定URL的照片

在报表中显示指定URL的照片

以下源码演示如何在ReportMachine报表中显示对应记录指定文件名称的照片,并且照片从指定的URL中下载。其原理是,在报表显示前(OnBeforePrint事件),找到要显示照片的TRMPictureView控件,再从揚hoto?#23383;段得到文件,从指定的URL下载该文件名称的照片(jpg文件)到临时文件夹,再将临时文件夹中的照片显示到TRMPictureView控件中。

需要注意的是,首先要在uses 子句中加入URLMon单元的引用。在窗口创建时得到临时文件,当然可以是其它具体的文件,只不过每次报表预览积累下来的照片会占用不少的磁盘空间。

主要代码在RMReport1的OnBeforePrint事件中,首先找到要显示的TRMPictureView控件,这个控件在设计ReportMachine报表时加入,并赋予名称(这里为損icPhoto?#65289;,代码中的名称要与报表中的名称对应,否则永远找不到。在找到控件后,读取保存照片文件名称的字段(这里为揚hoto?#65289;,再将指定的URL(这里定义为常数URL)加上文件名称作为完整的下载URL,必须是有效的URL,可以在服务器中指定对应的虚拟目录,事先在IE中测试是否有效。将文件下载到临时目录下相同名称的文件,再将该文件显示到picPhoto控件中。

该程序在Windows2000 Server + Delphi6 + ReportMachine3.0中测试过。已知的BUG:下载URL无效时响应会变得很慢,且DownLoadFile()函数的返回结果总为True。

unit u_frmURLReport;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, Grids, DBGrids, ADODB, ExtCtrls, RM_Dataset,
  RM_Class, RM_Designer, RM_Common, ComCtrls, URLMon;   // Use "URLMon" first!

type
  TfrmURLReport = class(TForm)
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    ADOQuery1EmpNo: TStringField;
    ADOQuery1EmpName: TStringField;
    ADOQuery1Company: TStringField;
    ADOQuery1ClassName: TStringField;
    ADOQuery1CardNo: TIntegerField;
    ADOQuery1Photo: TStringField;
    Panel1: TPanel;
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    RMReport1: TRMReport;
    RMDesigner1: TRMDesigner;
    RMDBDataSet1: TRMDBDataSet;
    StatusBar1: TStatusBar;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure RMReport1BeforePrint(Memo: TStrings; View: TRMReportView);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    PhotoPath: String;
  public
    { Public declarations }
  end;

var
  frmURLReport: TfrmURLReport;

implementation

uses u_Download;

{$R *.dfm}

{ Get the temporary folder }
function GetTempPathName: String;
var
  Buf: PChar;
begin
  Result:= '';
  GetMem(Buf, 255);
  try
    if GetTempPath(255, Buf) <> 0 then
      Result:= String(Buf);
  finally
    FreeMem(Buf, 255);
  end;
end;

{ Download file from the specified URL }
function DownloadFile(Source, Dest: string): Boolean;
begin
  try
    Result := UrlDownloadToFile(nil, PChar(source), PChar(Dest),
      0, nil) = 0;
  except
    Result := False;
  end;
end;

{ Filter the records }
procedure TfrmURLReport.Button1Click(Sender: TObject);
begin
  ADOQuery1.Filtered:= False;
  ADOQuery1.Filter:= Edit1.Text;
  ADOQuery1.Filtered:= True;
end;

{ Preview the report }
procedure TfrmURLReport.Button2Click(Sender: TObject);
begin
  RMReport1.LoadFromFile(ChangeFileExt(Application.ExeName, '.rmf'));
  RMReport1.ShowReport;
end;

{ Design the report }
procedure TfrmURLReport.Button3Click(Sender: TObject);
begin
  RMReport1.LoadFromFile(ChangeFileExt(Application.ExeName, '.rmf'));
  RMReport1.DesignReport;
end;

{ Download the specified photo and show on the report }
procedure TfrmURLReport.RMReport1BeforePrint(Memo: TStrings;
  View: TRMReportView);
const
  URL = 'http://infoserver/gkpic/gkpic/Ext/';
var
  picPhoto: TRMPictureView;
  FileName: String;
begin
  picPhoto:= (RMReport1.FindObject('picPhoto') as TRMPictureView);
  if picPhoto <> nil then
  begin
    FileName:= RMReport1.Dataset.GetFieldValue('Photo');        
    StatusBar1.SimpleText:= 'Downloading photos...';
    StatusBar1.Update;
    DownloadFile(URL + FileName, PhotoPath + FileName);
    if FileExists(PhotoPath + FileName) then
      picPhoto.Picture.LoadFromFile(PhotoPath + FileName)
    else
      picPhoto.Picture.Graphic:= nil;
  end;
end;

{ Get the temporary folder to save the photos first }
procedure TfrmURLReport.FormCreate(Sender: TObject);
begin
  PhotoPath:= GetTempPathName;
  //PhotoPath:= ExtractFilePath(Application.ExeName);
end;

end.


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
版本:1.0.0.1 更新日志: 1. 修正部分用户使用用到插件的分割符号,致使插件不能正常使用,本版间使用xml格式,不会和用户使用的符号有冲突。 2. 重新构件了ocx,jar两个地方的源码,使结构更 3. 增加图片、图表的支持 4。 增加了在线设计 5。增加对每个按纽是否可用的控制 注意事项: 1. 这个版本的功能和前一个版本的功能是一样的,如果运行得可以就可以不用更新 严重声名: 插件用到的第三方控件如果涉及版权问题,请自觉向其所再单位获得授权。 声明: 任何个人或组织可以自由使用,可以任意修改、复制、发布,但要保留本人的声明以遵重本人的劳动成果,并且不能用于任何商业目的。 本人提供的是免费报表,不与商家争长短,但希望对你有用: 使用方法: 范例使用的是access数据库,首先在odbc 注册ReportDemo.mdb 数据源,名字随你喜好, 然后修改classes文件下的 db.properties 的连接。 然后把项目放在tomcat下运行既可: 起作用的关键文件:RMVIEWP.CAB 文件,客户端插件 Lib下的两个包:rmview1.0.0.1.jar 使用接口: package common.rmview; /* * 设计的第一步,首先定义字段的类型, * 然后通过AddRow将结果加入缓存 * */ import common.rmview.surperInterface.IDataSet; import common.rmview.until.Types; public interface IRMDataSet extends IDataSet { public void addField(String fieldName, Types.filedType type, int fieldLength, Boolean isNull);//1:将段类型加入 public void addField(String fieldName, Types type, int fieldLength);//1:将段类型加入,isNull 默认为 false public void AddRow(IRMDataRecord rmd);//2:在 IRMDataRecord实现类赋值完成一条记录后使用一次,循环加入. } package common.rmview; import common.rmview.surperInterface.IDataRecord; /* * 使用时的第二步 * 通过这个接口设置结果集 */ public interface IRMDataRecord extends IDataRecord { public void SetValue(String fieldName,Object objValue); } package common.rmview; /* * 第三步: * 前面IDataSet,IRMDataRecord实现类完成后,通过此接口的实现类向html也面输出ie到ie以方便ocx的取值使用 */ public interface IRMEngine { final int rmf=1; final int rls=2; public void SetReportFile(String urlandFileName,int fileFormat);//1:设置报表路径及文件名称 public void AddClientDataSet(String dataSourceName,IRMDataSet rmd);//2:设置数据源名称以ocx使用,并把IRMDataSet实现的结果加入 public String CreateViewer();//3:生成在html页面的编码 } 具体的使用方法请参照 index.jsp 报表的使用方法请自己参照官方的文档 RM报表的设计请另行学习。 需要更高版本 请联系作者购买 新建QQ群:29672297 有疑问请发邮件到 lujianfu123@yahoo.com.cn 下期计划:进行对 fastReport的封装。不过也要看呼声如何再决定。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值