6
6rl
Unregistered / Unconfirmed
GUEST, unregistred user!
我用Intraweb做的是一个预览文档窗口
我的想法是这样的
先把word文件读进TMemoryStream中
然后用Response把TMemoryStream发给客户段IE
已经设好Response.ContentType := 'application/Msword';
如果用sendStream方法
返回的是乱码
intraweb 7.0
新建一个Intraweb Application
修改ServerController
unit ServerController;
interface
uses
SysUtils, Classes, IWServerControllerBase, IWBaseForm, HTTPApp,
// For OnNewSession Event
UserSessionUnit, IWApplication, IWAppForm;
type
TAByte=array [0..2047] of byte;//在TIWServerController里声明
TIWServerController = class(TIWServerControllerBase)
procedure IWServerControllerBaseNewSession(ASession: TIWApplication;
var VMainForm: TIWBaseForm);
procedure IWServerControllerBaseBeforeDispatch(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
private
public
end;
function UserSession: TIWUserSession;
implementation
{$R *.dfm}
uses
IWInit;
function UserSession: TIWUserSession;
begin
Result := TIWUserSession(WebApplication.Data);
end;
procedure TIWServerController.IWServerControllerBaseNewSession(
ASession: TIWApplication; var VMainForm: TIWBaseForm);
begin
ASession.Data := TIWUserSession.Create(nil);
end;
procedure TIWServerController.IWServerControllerBaseBeforeDispatch(
Sender: TObject; Request: TWebRequest; Response: TWebResponse;
var Handled: Boolean);
var
M : TMemoryStream;
fr: TFileStream;
FileRead: String;
buffer: TAByte;
readBytes, size: Integer;
pathName : String;
begin
pathName := Request.PathInfo;
Handled := true;//自己处理,不用intraweb
FileRead := 'E:/test.doc';
if not FileExists(FileRead) then
begin
exit;
end;
M := TMemoryStream.Create;
try
fr := TFileStream.Create(FileRead, fmShareDenyWrite );
fr.Seek(0,0);
size := fr.Size;
readBytes := 0;
while true do
begin
size := size - readBytes;
if size <=0 then
begin
break;
end;
readBytes := fr.Read(buffer, SizeOf(buffer));
if readBytes = 0 then
begin
break;
end;
if readBytes > 0 then
begin
M.Write(buffer,readBytes);
end;
end;
Response.ContentType := 'application/Msword';
Response.SendStream(M); //显示乱码
// Response.ContentStream := M; //显示正常,在某种情况会报错
//project Project1.exe rasised exception class Exception with message
//'Unrecognized Command::_VTI_INF.HTML:'.Process stopped.Use Step or
//Run to continue.出这种情况会话崩溃结束.
Finally
fr.Destroy;
End;
end;
initialization
TIWServerController.SetServerControllerClass;
end.
我的想法是这样的
先把word文件读进TMemoryStream中
然后用Response把TMemoryStream发给客户段IE
已经设好Response.ContentType := 'application/Msword';
如果用sendStream方法
返回的是乱码
intraweb 7.0
新建一个Intraweb Application
修改ServerController
unit ServerController;
interface
uses
SysUtils, Classes, IWServerControllerBase, IWBaseForm, HTTPApp,
// For OnNewSession Event
UserSessionUnit, IWApplication, IWAppForm;
type
TAByte=array [0..2047] of byte;//在TIWServerController里声明
TIWServerController = class(TIWServerControllerBase)
procedure IWServerControllerBaseNewSession(ASession: TIWApplication;
var VMainForm: TIWBaseForm);
procedure IWServerControllerBaseBeforeDispatch(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
private
public
end;
function UserSession: TIWUserSession;
implementation
{$R *.dfm}
uses
IWInit;
function UserSession: TIWUserSession;
begin
Result := TIWUserSession(WebApplication.Data);
end;
procedure TIWServerController.IWServerControllerBaseNewSession(
ASession: TIWApplication; var VMainForm: TIWBaseForm);
begin
ASession.Data := TIWUserSession.Create(nil);
end;
procedure TIWServerController.IWServerControllerBaseBeforeDispatch(
Sender: TObject; Request: TWebRequest; Response: TWebResponse;
var Handled: Boolean);
var
M : TMemoryStream;
fr: TFileStream;
FileRead: String;
buffer: TAByte;
readBytes, size: Integer;
pathName : String;
begin
pathName := Request.PathInfo;
Handled := true;//自己处理,不用intraweb
FileRead := 'E:/test.doc';
if not FileExists(FileRead) then
begin
exit;
end;
M := TMemoryStream.Create;
try
fr := TFileStream.Create(FileRead, fmShareDenyWrite );
fr.Seek(0,0);
size := fr.Size;
readBytes := 0;
while true do
begin
size := size - readBytes;
if size <=0 then
begin
break;
end;
readBytes := fr.Read(buffer, SizeOf(buffer));
if readBytes = 0 then
begin
break;
end;
if readBytes > 0 then
begin
M.Write(buffer,readBytes);
end;
end;
Response.ContentType := 'application/Msword';
Response.SendStream(M); //显示乱码
// Response.ContentStream := M; //显示正常,在某种情况会报错
//project Project1.exe rasised exception class Exception with message
//'Unrecognized Command::_VTI_INF.HTML:'.Process stopped.Use Step or
//Run to continue.出这种情况会话崩溃结束.
Finally
fr.Destroy;
End;
end;
initialization
TIWServerController.SetServerControllerClass;
end.