⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cb200006ne_f.asp.htm

📁 C++builder学习资料C++builder
💻 HTM
📖 第 1 页 / 共 4 页
字号:
    

<p class=BodyText> &nbsp; </p>    

    

<p class=Code><span class=Code>&lt;form    

method=&quot;POST&quot; </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;action=&quot;http://elmanova/scripts/formproc.exe/t3&quot;&gt; </span></p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=BodyText> The <i    

style='mso-bidi-font-style:normal'>action</i> parameter contains a URL for the    

resource, which should be requested by pressing the Submit button. In this case, it's the same application, but with <i    

style='mso-bidi-font-style:normal'>PathInfo</i> assigned the value "/t3". </p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=BodyText> Save and    

compile this project. After filling in this form and pressing the Submit button, we obtain a dynamically generated page whose content uses    

the data of the form (see Figure 14). </p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=Captions><img    

 width=250 height=131 src="images/cb200006ne_f_image025.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image025.gif"><br>     

<b>Figure 14:</b> The result of processing the user    

entry. </p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=BodyText> Of    

course, we can create an ISAPI DLL using this application. To do so, we must    

create a new Web server application, delete its Web module, and add another one    

from the previous project. </p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=Subheads>     

Using Databases in Web    

Applications</p>    

    

<p class=BodyText> We can    

insert code for adding records with user data to the database table to <i    

style='mso-bidi-font-style:normal'>WebModule1WebActionItem3Action</i> (and thus    

create a simple mailing list). To do this, we can add some data access    

components to <i>WebModule1</i>, and bind    

them to each other by setting the appropriate properties (see Figure 15). </p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=Captions> <img    

 width=221 height=169 src="images/cb200006ne_f_image026.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image026.gif"><br>     

<b>Figure 15:</b>    

<i>TWebModule</i> with DataAccess components    

for collecting user entries. </p>    

    

<p class=BodyText> &nbsp; </p>    

    

    

    

<p class=BodyText> Now, we    

can rewrite the third <i>OnAction</i> event    

handler: </p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=Code><span class=Code><b>void</b> <b    

style='mso-bidi-font-weight:normal'>__fastcall</b>    

TWebModule1::WebModule1WebActionItem3Action(</span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;TObject *Sender, TWebRequest *Request, </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;TWebResponse *Response, <b>bool</b> &amp;Handled) </span></p>    

    

<p class=Code><span class=Code>{</span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;AnsiString CustName =    

Request-&gt;QueryFields-&gt;Values[&quot;T1&quot;] +</span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;&nbsp;&nbsp;Request-&gt;ContentFields-&gt;Values[&quot;T1&quot;]; </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;AnsiString CustEmail =    

Request-&gt;QueryFields-&gt;Values[&quot;T2&quot;] +</span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;&nbsp;&nbsp;Request-&gt;ContentFields-&gt;Values[&quot;T2&quot;]; </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;Table1-&gt;Open();</span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;Table1-&gt;Append();</span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;Table1-&gt;FieldByName(&quot;Name&quot;)-&gt;Value = CustName; </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;Table1-&gt;FieldByName(&quot;E-mail&quot;)-&gt;Value = CustEmail; </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;Table1-&gt;Post();</span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;Table1-&gt;Close();</span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;Response-&gt;Content =    

PageProducer2-&gt;Content();</span></p>    

    

<p class=Code><span class=Code>}</span></p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=BodyText> Now our    

application adds records with user data to <i>Table1</i>.    

Compile, save, test, and close the project. </p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=BodyText> That was    

a simple example of using databases. More often, it's interesting to present    

the data from databases in a browser. For this purpose, we can use the <i    

style='mso-bidi-font-style:normal'>TDataSetTableProducer</i> component, which    

is responsible for obtaining data from <i>TDataSet</i>    

components and presenting them in the form of HTML tables. </p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=BodyText> To do    

this, we can use the DB Web Application Wizard from the New Items dialog box,    

as shown in Figure 16. </p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=Captions> <img    

 width=250 height=206 src="images/cb200006ne_f_image028.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image028.gif"><br>     

<b>Figure 16:</b>    

Selecting the DB Web Application Wizard from the New Items dialog box. </p>    

    

<p class=BodyText> &nbsp; </p>    

    

    

    

<p class=BodyText> This    

wizard will display a series of dialog boxes similar to those of the Database    

Form Wizard. We must fill them in, choosing the database, the table, and the    

fields to publish (see Figure 17). </p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=Captions> <img    

 width=250 height=161 src="images/cb200006ne_f_image030.jpg" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image030.jpg"> <br>    

<b>Figure 17:</b>    

Selecting the fields to represent in a browser. </p>    

    

<p class=BodyText> &nbsp; </p>    

    

    

    

<p class=BodyText> After    

filling in all the forms, we'll obtain a WebModule with Table (or Query),    

Session, and DataSetPageProducer components. This is enough to create a simple    

Web application, like the one shown in Figure 18. </p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=Captions> <img    

 width=250 height=208 src="images/cb200006ne_f_image032.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image032.gif"><br>     

<b>Figure 18:</b>    

The result of publishing data in the Web. </p>    

    

<p class=BodyText> &nbsp; </p>    

    

    

    

<p class=BodyText> This    

table (biolife.db from the BCDEMOS database) contains a graphic field, and it    

would be interesting to view it in a table. How can we do this? </p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=BodyText> One    

solution would be to create a calculated field that contains an appropriate    

HTML tag with a reference to the image obtained from this graphic field. So    

let's create a new calculated string field for <i>Table1</i> that will contain a reference to the image that was    

previously extracted from the database. We also need to take into account that    

there could be several users that call our Web application simultaneously, so    

we should prevent downloading image files produced by another user. One    

mechanism for this would be to give unique names to image files. For example,    

the <i>OnCalcField</i> event handler for the    

<i>Table1</i> component can be the    

following: </p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=Code><span class=Code><b>void</b> <b    

style='mso-bidi-font-weight:normal'>__fastcall</b>    

TWebModule1::Table1CalcFields(</span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;TDataSet *DataSet) </span></p>    

    

<p class=Code><span class=Code>{</span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;TDateTime DT = Now();</span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;Word hour, min, sec, msec; </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;DecodeTime(DT, hour, min, sec, msec); </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;<i> <span Class=CodeBlue>// Create a unique ID based on current time. </span></i></span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;UserID = IntToStr(hour) + IntToStr(min) +</span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IntToStr(sec) + IntToStr(msec); </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;<i> <span Class=CodeBlue>// Create a unique image name. </span></i></span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;<b> int</b>    

i = random(1000000000); </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;AnsiString FileName = UserID + &quot;_&quot;    

+ IntToStr(i) + &quot;.JPG&quot;; </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;<i> <span Class=CodeBlue>// Save an image to a file. </span></i></span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;TJPEGImage *JPG = <b>new</b> TJPEGImage; </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;TPicture *P = <b>new</b> TPicture();</span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;P-&gt;Assign(Table1-&gt;FieldByName(&quot;Graphic&quot;)); </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;JPG-&gt;Assign(P-&gt;Graphic); </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;JPG-&gt;SaveToFile(FileName); </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;Table1Picture-&gt;Value = &quot;&lt;IMG    

SRC='&quot; + FileName + &quot;'&gt;&quot;; </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;&amp;P-&gt;Free; </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;&amp;JPG-&gt;Free; </span></p>    

    

<p class=Code><span class=Code>}</span></p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=BodyText> We must    

also refer to the appropriate header file: </p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=Code><span class=Code><span Class=CodeGrn>#include    

&lt;vcl\jpeg.hpp&gt; </span></span></p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=BodyText> This    

code supposes that any two users cannot extract the same image file from the    

database during the same millisecond. In this case, we can generate the image    

file name combined from a millisecond-based "timestamp" and a random value that    

is unique for this user session, but unfortunately is not unique for several    

users. </p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=BodyText> Why is    

the image file not deleted in this event handler? Image files can be downloaded    

for a long time - especially if the user of the application uses a dial-up    

connection. This means that these files must stay on the Web server hard drive,    

even when the Web application has finished its job. Therefore, they should be    

deleted by another application, or, if you have created the only application    

for your Web server, by another instance of the same application that is    

running later. This example shows how, assuming that ten minutes is enough time    

to download all the necessary images: </p>    

    

<p class=BodyText> &nbsp; </p>    

    

<p class=Code><span class=Code><b>void    

__fastcall</b>    

TWebModule1::WebModuleCreate(TObject *Sender) </span></p>    

    

<p class=Code><span class=Code>{</span></p>    

    

<p class=Code><span class=Code><i><span Class=CodeBlue>&nbsp;&nbsp;// Delete old    

*.JPG files</span></i></span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;TSearchRec sr; </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;<b> if</b>    

(FindFirst(&quot;*.JPG&quot;, faAnyFile, sr) == 0) </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;{ </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;&nbsp;&nbsp;<b> do</b></span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;&nbsp;&nbsp;{ </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b> if</b>    

(Double(Now()) - Double(FileDateToDateTime(sr.Time)) </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt; 0.00694) </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i> <span Class=CodeBlue>// Ten minutes is approximately</span></i></span></p>    

    

<p class=Code><span class=Code><i><span Class=CodeBlue>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// a    

0.00694 part of a day</span></i></span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DeleteFile(sr.Name); </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;&nbsp;&nbsp;} </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;&nbsp;&nbsp;<b> while</b>    

(FindNext(sr) == 0); </span></p>    

    

<p class=Code><span class=Code>&nbsp;&nbsp;&nbsp;&nbsp;FindClose(sr); </span></p>    

    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -