把我本地的word文件保存到服务器的数据库中,然后还要在客户端打开这个word文件 ( 积分: 100 )

  • 主题发起人 zhangxuepu107
  • 开始时间
Z

zhangxuepu107

Unregistered / Unconfirmed
GUEST, unregistred user!
把我本地的word文件保存到服务器的数据库中,然后还要在客户端打开这个word文件
打开的时候是乱码?不知是什么原因
写入:
if OpenDialog1.Execute then
begin
if OpenDialog1.FileName ='' then
exit;
with ds do
begin
close;
commandtext:='select * from cc';
open;
end;
ds.insert;
fs:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead);
fs.Seek(0,0);
//ds.FieldByName('htnum').asstring:=edit2.text;
TBlobField(ds.FieldByName('htyj')).LoadFromStream(fs);
ds.Post;
ds.Close;
fs.Free;
showmessage('ok');
end;
读出:
with tongyong do
begin
if active then
active:=false;
sql.clear;
sql.add('select * from cc ');
active:=true;
fs:=TAdoBlobStream.Create(TBlobField(FieldbyName('htyj')),bmRead);//将信息读出同时写到流中
fn:='temp_word.doc';
fs.SaveToFile(fn);
//保存为文件
ShellExecute(0, 'open', PChar(fn), nil, nil, SW_SHOW);//打开
end;
请指点,急急急!!!!!! qq:85277206
 
用内存流试试
 
用内存流也是出现乱码....
 
知道就回答一下,不知道就顶一下,就是为了混点分。
 
我也曾遇到我和楼主同样的问题,我的解决办法如下。这是我用在一项目上的源码,应该对楼主有帮助(楼主给的分啊[:(!]):
//从数据库中下载试题附件文件到考生目录下
procedure do
wnload_Stfj_From_DB;
var
fn:string;
begin

//......
with Stfjb_Clientdataset do
begin
close;
commandtext := 'select * from 题库附件表 where PID in '
+'(select 题目ID from 考生试卷表 where 课程号='+quotedstr(KCDM)+' and 学号='+quotedstr(KS_NUM)+')';
Open;
first;
while not eof do
begin
fn := Fieldbyname('试题附件文件名').AsString;

if (fn<>'') and (not Fieldbyname('试题附件').IsNull) then
begin
fn := KS_PATH+'/'+fn;
TBlobField(Fieldbyname('试题附件')).SaveToFile(fn);
end;
Next;
end;
end;
//....
end;

//保存试题附件文件到数据库中去
procedure Save_Stfj_To_DB;
var
fn:string;
//mystream:Tmemorystream;
begin
try
//mystream := TMemoryStream.Create;
with Stfjb_Clientdataset do
begin
close;
commandtext := 'select * from 题库附件表 where pid in '
+'(select 题目id from 考生试卷表 where 课程号='+quotedstr(KCDM)+' and 学号='+quotedstr(KS_NUM)+')'
+' and 是否回收=1';
Open;
first;
while not eof do
begin
fn := Fieldbyname('试题附件文件名').AsString;
fn := KS_PATH+'/'+fn;

if FileExists(fn) then
begin
Update_ClientDataSet.Append;
Update_ClientDataSet.Fieldbyname('学号').AsString := KS_NUM;
Update_ClientDataSet.Fieldbyname('课程号').AsString := KCDM;
Update_ClientDataSet.Fieldbyname('答案附件文件名').AsString := ExtractFileName(fn);
try
TBlobField(Update_ClientDataSet.FieldByName('答案附件')).LoadFromFile(fn);
Update_ClientDataSet.Post;
except
Next;
end;
//本想用下面的Insert内存流方式,执行效率要好一些,可惜有问题,原因不详!因此没办法只好用上面的Append的方式。
{//=========================================================================
try
Update_ClientDataSet.CommandText := 'Insert into 答案附件表 (学号,课程号,答案附件文件名,答案附件) Values:)xh,:kch,:dafn,:dafile)';
Update_ClientDataSet.Params.ParamByName('xh').AsString:=KS_NUM;
Update_ClientDataSet.Params.ParamByName('kch').AsString:=KCDM;
Update_ClientDataSet.Params.ParamByName('dafn').AsString:=ExtractFileName(fn);
Update_ClientDataSet.Params.ParamByName('dafile').LoadFromStream(Mystream,ftBlob);
Update_ClientDataSet.Execute;
except
Update_ClientDataSet.Close;
Next;
end;
//===========================================================================
}
end;
//end if FileExists then
.....
Next;
end;
Update_ClientDataSet.ApplyUpdates(0);
Update_ClientDataSet.Close;
end;
finally
//Mystream.Free;
end;
end;

更多问题见 http://xieyunc.blog.163.com/
 
以前也出现过,可能的原因是OFFICE文档结构是保密的,所以只能用LoadFromFile保存
 
用文件流或内存流试一试。
 
注意数据库中的存储文件的字段数据类型。有些类型会改变文件,具体记不清了:)。
 
to: xieyunc
我按照你的方式改了一下,还是乱码,那是什么原因呀,望指点~~!!!
 
to: xieyunc
你的数据库类型是 text类型吗?
 
不是,字段是image类型
 
多人接受答案了。
 
to: xieyunc
我在 2000 server 下开发的软件在 xp 下运行有的地方报错,不知是什么原因,能不能指点一下,再有就是c/s 下的软件,有的电脑能ping 通s,但是运行软件提示连接不上,这又是怎么回事呀.
QQ:85277206
 
连接MS SQL Server 吗?如果是的话,请安装mdac2.8
 
to zhangxuepu107,
我在BLOG已把你的代码改了一下,你去看看 http://xieyunc.blog.163.com/
 
顶部