Subject | Generation of dataset for inet download (?) |
---|---|
Author | Claus Thor Barth |
Post date | 2001-04-14T18:04:07Z |
Hi,
I hope you can help me. I have a site where users can do different
db-queries, and download the result as a text file.
The source is compiled as an ISAPI dll to do the trick.
This works OK. But sometimes the download does not correspond to the
resultset, when several users are working simultaneously.
Maybe my use of MemoryStreams are not the best solution. If you have
another idea please let me know.
I have included the source for one the routines (sorry for the danish words
:)
Best regards and thanks in advance
/Claus
procedure TWebModule1.WebModule1WebActionItem42Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var i :integer;
SQLstrA, SQLstrB, SQLstrC, tmpStrA, tmpStrB : string;
begin
if CheckCookie then
begin
if opslag = '' then
begin
SQLstrA := ''; SQLstrB := ''; SQLstrC := '';
iboquery14.close;
iboquery14.SQL.clear;
iboquery14.SQL.add('select distinct
KOMMUNE,FORVALTNING,STILLING,FENAVN,ADRES1,
PNR,CITY,tlf,fax,mail from
indkm,komnavn');
iboquery14.sql.add('where (indkm.komkode = komnavn.komkode)');
for i := 1 to 10 do
begin
if request.ContentFields.Values['A'+inttostr(i)] <> '' then
if SQLstrA = '' then SQLstrA := SQLstrA +
'"'+request.ContentFields.Values['A'+inttostr(i)]+'"'
else SQLstrA := SQLstrA +
',"'+request.ContentFields.Values['A'+inttostr(i)]+'"';
end;
for I := 0 to request.ContentFields.Count - 1 do
begin
if Pos ('=', request.ContentFields[I]) > 1 then
begin
tmpStrA := request.ContentFields.Strings[i];
tmpStrB := copy(tmpStrA,4,length(tmpStrA));
if request.ContentFields.Names[I] = 'B1' then
if SQLstrB = '' then SQLstrB := SQLstrB + '"'+tmpStrB+'"'
else SQLstrB := SQLstrB + ',"'+tmpStrB+'"';
end;
end;
if SQLstrA <> '' then
begin
iboquery14.SQL.Add(' and ');
iboquery14.sql.add('((kat1 in ('+sqlstrA+')) or');
iboquery14.sql.add(' (kat2 in ('+sqlstrA+')) or');
iboquery14.sql.add(' (kat3 in ('+sqlstrA+')) or');
iboquery14.sql.add(' (kat4 in ('+sqlstrA+')) or');
iboquery14.sql.add(' (kat5 in ('+sqlstrA+')))');
end;
if request.ContentFields.Values['C0'] = '' then
begin
for i := 1 to 17 do
begin
if request.ContentFields.Values['C'+inttostr(i)] <> '' then
if SQLstrC = '' then SQLstrC := SQLstrC + '(komkode
between '+request.ContentFields.Values['C'+inttostr(i)]+')'
else SQLstrC := SQLstrC + ' or (komkode between
'+request.ContentFields.Values['C'+inttostr(i)]+')';
end;
if (SQLstrC <> '') and (SQLstrB <> '') then
iboquery14.sql.add('and (kommune in ('+sqlstrB+') or ('+sqlstrC+'))');
if (SQLstrC <> '') and (SQLstrB = '') then iboquery14.sql.add('and
('+sqlstrC+')');
if (SQLstrC = '') and (SQLstrB <> '') then iboquery14.sql.add('and
(kommune in ('+sqlstrB+'))');
end;
iboquery14.Open;
response.content := ppFlettefil6.Content;
end
else response.content := KunOpslag;
end
else response.content := HuskLogon;;
handled := true;
end;
procedure TWebModule1.ppFlettefil6HTMLTag(Sender: TObject; Tag: TTag;
const TagString: String; TagParams: TStrings; var ReplaceText: String);
var
FlettefilListen : String;
begin
if CompareText(TagString, 'FLETTEFIL') = 0 then
begin
FlettefilListen := '';
FlettefilListen := FlettefilListen + '<p>Download flettefil : <a
href="./dnload6">Flettefil</a><p>';
FlettefilListen := FlettefilListen + '<b>Antal adresser i flettefilen :
'+inttostr(iboquery14.RecordCount)+'</b><br>';
FlettefilListen := FlettefilListen + 'Efter klik på Download Flettefil
kan hele filen ses ved åbning af den i<br>';
FlettefilListen := FlettefilListen + 'WordPad. Det kan ske, at en
tilligere valgt fil vises først. Gentag derfor<br>';
FlettefilListen := FlettefilListen + 'åbningen, til den rigtige fil er
på skærmen, og giv den nyt navn (Gem som).<br>';
FlettefilListen := FlettefilListen + 'Se i øvrigt under "Hjælp".<p>';
FlettefilListen := FlettefilListen + '<Table width="100%" Align="Left"
Border=0>'+
'<TR
BGCOLOR="Lime"><TD><b>Felt1</b></td><TD><b>Felt2</b></td><TD><b>Felt3</b></t
d><TD><b>Felt4</b></td>';
iboquery14.First;
while not iboquery14.Eof do
begin
FlettefilListen := FlettefilListen + '<tr><td>'+
iboquery14.fieldbyname('kommune').asstring +'
Kommune</td><td>'+
iboquery14.fieldbyname('forvaltning').asstring+'</td><td>'+
iboquery14.fieldbyname('fenavn').asstring+'</td><td>'+
iboquery14.fieldbyname('stilling').asstring+'</td>';
iboquery14.Next;
end;
FlettefilListen := FlettefilListen + '</table>';
end;
ReplaceText := FlettefilListen;
end;
procedure TWebModule1.WebModule1WebActionItem43Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
S: TMemoryStream;
SList : TStringList;
begin
SList := TStringList.Create;
iboquery14.first;
while not iboquery14.eof do
begin
SList.Add('"'+
iboquery14.fieldbyname('kommune').asstring+'
Kommune";"'+
iboquery14.fieldbyname('forvaltning').asstring+'";"'+
iboquery14.fieldbyname('stilling').asstring+'";"'+
iboquery14.fieldbyname('fenavn').asstring+'";"'+
iboquery14.fieldbyname('adres1').asstring+'";"'+
iboquery14.fieldbyname('pnr').asstring+'";"'+
iboquery14.fieldbyname('city').asstring+'";"'+
iboquery14.fieldbyname('tlf').asstring+'";"'+
iboquery14.fieldbyname('fax').asstring+'";"'+
iboquery14.fieldbyname('mail').asstring+'"');
iboquery14.next;
end;
S := TMemoryStream.Create;
try
SList.SaveToStream(S);
S.Position := 0;
Response.ContentType := 'application/x-dvi';
Response.ContentStream := S;
Response.SendResponse;
finally
SList.Free;
end;
end;
I hope you can help me. I have a site where users can do different
db-queries, and download the result as a text file.
The source is compiled as an ISAPI dll to do the trick.
This works OK. But sometimes the download does not correspond to the
resultset, when several users are working simultaneously.
Maybe my use of MemoryStreams are not the best solution. If you have
another idea please let me know.
I have included the source for one the routines (sorry for the danish words
:)
Best regards and thanks in advance
/Claus
procedure TWebModule1.WebModule1WebActionItem42Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var i :integer;
SQLstrA, SQLstrB, SQLstrC, tmpStrA, tmpStrB : string;
begin
if CheckCookie then
begin
if opslag = '' then
begin
SQLstrA := ''; SQLstrB := ''; SQLstrC := '';
iboquery14.close;
iboquery14.SQL.clear;
iboquery14.SQL.add('select distinct
KOMMUNE,FORVALTNING,STILLING,FENAVN,ADRES1,
PNR,CITY,tlf,fax,mail from
indkm,komnavn');
iboquery14.sql.add('where (indkm.komkode = komnavn.komkode)');
for i := 1 to 10 do
begin
if request.ContentFields.Values['A'+inttostr(i)] <> '' then
if SQLstrA = '' then SQLstrA := SQLstrA +
'"'+request.ContentFields.Values['A'+inttostr(i)]+'"'
else SQLstrA := SQLstrA +
',"'+request.ContentFields.Values['A'+inttostr(i)]+'"';
end;
for I := 0 to request.ContentFields.Count - 1 do
begin
if Pos ('=', request.ContentFields[I]) > 1 then
begin
tmpStrA := request.ContentFields.Strings[i];
tmpStrB := copy(tmpStrA,4,length(tmpStrA));
if request.ContentFields.Names[I] = 'B1' then
if SQLstrB = '' then SQLstrB := SQLstrB + '"'+tmpStrB+'"'
else SQLstrB := SQLstrB + ',"'+tmpStrB+'"';
end;
end;
if SQLstrA <> '' then
begin
iboquery14.SQL.Add(' and ');
iboquery14.sql.add('((kat1 in ('+sqlstrA+')) or');
iboquery14.sql.add(' (kat2 in ('+sqlstrA+')) or');
iboquery14.sql.add(' (kat3 in ('+sqlstrA+')) or');
iboquery14.sql.add(' (kat4 in ('+sqlstrA+')) or');
iboquery14.sql.add(' (kat5 in ('+sqlstrA+')))');
end;
if request.ContentFields.Values['C0'] = '' then
begin
for i := 1 to 17 do
begin
if request.ContentFields.Values['C'+inttostr(i)] <> '' then
if SQLstrC = '' then SQLstrC := SQLstrC + '(komkode
between '+request.ContentFields.Values['C'+inttostr(i)]+')'
else SQLstrC := SQLstrC + ' or (komkode between
'+request.ContentFields.Values['C'+inttostr(i)]+')';
end;
if (SQLstrC <> '') and (SQLstrB <> '') then
iboquery14.sql.add('and (kommune in ('+sqlstrB+') or ('+sqlstrC+'))');
if (SQLstrC <> '') and (SQLstrB = '') then iboquery14.sql.add('and
('+sqlstrC+')');
if (SQLstrC = '') and (SQLstrB <> '') then iboquery14.sql.add('and
(kommune in ('+sqlstrB+'))');
end;
iboquery14.Open;
response.content := ppFlettefil6.Content;
end
else response.content := KunOpslag;
end
else response.content := HuskLogon;;
handled := true;
end;
procedure TWebModule1.ppFlettefil6HTMLTag(Sender: TObject; Tag: TTag;
const TagString: String; TagParams: TStrings; var ReplaceText: String);
var
FlettefilListen : String;
begin
if CompareText(TagString, 'FLETTEFIL') = 0 then
begin
FlettefilListen := '';
FlettefilListen := FlettefilListen + '<p>Download flettefil : <a
href="./dnload6">Flettefil</a><p>';
FlettefilListen := FlettefilListen + '<b>Antal adresser i flettefilen :
'+inttostr(iboquery14.RecordCount)+'</b><br>';
FlettefilListen := FlettefilListen + 'Efter klik på Download Flettefil
kan hele filen ses ved åbning af den i<br>';
FlettefilListen := FlettefilListen + 'WordPad. Det kan ske, at en
tilligere valgt fil vises først. Gentag derfor<br>';
FlettefilListen := FlettefilListen + 'åbningen, til den rigtige fil er
på skærmen, og giv den nyt navn (Gem som).<br>';
FlettefilListen := FlettefilListen + 'Se i øvrigt under "Hjælp".<p>';
FlettefilListen := FlettefilListen + '<Table width="100%" Align="Left"
Border=0>'+
'<TR
BGCOLOR="Lime"><TD><b>Felt1</b></td><TD><b>Felt2</b></td><TD><b>Felt3</b></t
d><TD><b>Felt4</b></td>';
iboquery14.First;
while not iboquery14.Eof do
begin
FlettefilListen := FlettefilListen + '<tr><td>'+
iboquery14.fieldbyname('kommune').asstring +'
Kommune</td><td>'+
iboquery14.fieldbyname('forvaltning').asstring+'</td><td>'+
iboquery14.fieldbyname('fenavn').asstring+'</td><td>'+
iboquery14.fieldbyname('stilling').asstring+'</td>';
iboquery14.Next;
end;
FlettefilListen := FlettefilListen + '</table>';
end;
ReplaceText := FlettefilListen;
end;
procedure TWebModule1.WebModule1WebActionItem43Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
S: TMemoryStream;
SList : TStringList;
begin
SList := TStringList.Create;
iboquery14.first;
while not iboquery14.eof do
begin
SList.Add('"'+
iboquery14.fieldbyname('kommune').asstring+'
Kommune";"'+
iboquery14.fieldbyname('forvaltning').asstring+'";"'+
iboquery14.fieldbyname('stilling').asstring+'";"'+
iboquery14.fieldbyname('fenavn').asstring+'";"'+
iboquery14.fieldbyname('adres1').asstring+'";"'+
iboquery14.fieldbyname('pnr').asstring+'";"'+
iboquery14.fieldbyname('city').asstring+'";"'+
iboquery14.fieldbyname('tlf').asstring+'";"'+
iboquery14.fieldbyname('fax').asstring+'";"'+
iboquery14.fieldbyname('mail').asstring+'"');
iboquery14.next;
end;
S := TMemoryStream.Create;
try
SList.SaveToStream(S);
S.Position := 0;
Response.ContentType := 'application/x-dvi';
Response.ContentStream := S;
Response.SendResponse;
finally
SList.Free;
end;
end;