Use Delphi to develop files for the ASP development file and the specific implementation process

xiaoxiao2021-03-06  50

ASP (Active Server Page) is Microsoft's products, because it is easy to play, can quickly develop functional dynamic websites, now many websites (especially intranet / extranet internal network) use NT IIS ASP mode, making ASPs become current Amazing website development scripting language. In a web service, the file upload service is a very common feature, while the PWS under Win9X does not provide related components; IIS under NT provides a Post Acceptor component, but because it is to check the user's WWW access rights. Too easy to use; you can download the relevant components from the Internet, but most of them are commercial components, which are used to download the trial version, which is limited in the use time or function. Since ASP can call standard OLE / COM components, we can use high-level programming tools such as VB / VC / DELPHI to customize their own ASP file upload components based on our own requirements to meet their application system requirements.

The principle and concrete implementation process of using Delphi to develop files with Delphi will be discussed.

First, the principle of implementation of file upload

Upload based on web mode, to follow the RFC1867 standard, the uploaded file data is no exception. To select upload files with the following HTML page file (Delphiup.htm):

file upload </ title> </ head> <body></p> <p>Upload the file upload components in Delphi to implement file upload</p> <p><form name = "UPLOADFORM" action = "delphiup.asp" method = "post" enctype = "multipart / form-data"></p> <p><p> Save Save As: <Input Type = Text Name = "Saveas"></p> <p><p> To select the file upload: <input type = file name = "fileData"></p> <p><input type = "submit" name = "b1" value = "confirm upload"> </ p></p> <p></ form> </ body> </ html></p> <p>When the client selects a file (such as Test.txt, its content is "is a content for the file used to upload.") And press</p> <p>After the "Confirm Upload" button submits the data, the data received by the server server will have the following form:</p> <p>---------------------------- 7CF1D6C47C # 13 # 10</p> <p>Content-disposition: form-data; name = "saveas" # 13 # 10 # 13 # 10</p> <p>NewFileName # 13 # 10</p> <p>---------------------------- 7CF1D6C47C # 13 # 10</p> <p>Content-disposition: form-data; name = "filedata"; filename = "d: /test.txt"</p> <p>Content-Type: Text / Plain # 13 # 10 # 13 # 10</p> <p>Here is the content of the file used to upload. # 13 # 10</p> <p>---------------------------- 7CF1D6C47C # 13 # 10</p> <p>Content-disposition: form-data; name = "b1" # 13 # 10 # 13 # 10</p> <p>Confirm upload # 13 # 10</p> <p>---------------------------- 7CF1D6C47C--</p> <p>Among them, "------------------------ 7CF1D6C47C" is a delimiter, used in separating the domains in the form (FORM);</p> <p># 13 # 10 is a Delphi represented by the carriage return. We can think that the information description of each form field is started with a pair of carriage return lines # 13 # 10; the form field name begins with "Name =", "" "is ended; Value with two pairs of carriage return, backhand, starting with a pair of carriage return lines # 13 # 10 # Add a division of dividend; the file name begins with "filename =", "" " To end. With these logo, we can get the names and values ​​of the form field and the name of the file to be uploaded, so that the file data is read and stored.</p> <p>Second, the implementation process of file upload</p> <p>After understanding the above-mentioned data format, you have written a file to upload components for us is no difficult.</p> <p>(1) Beginning an engineering of an ASP component</p> <p>If you are not familiar with the steps develop Ole Automation Server with Delphi, see "Electronic and Computer" No. 06 of 1999 "An Ole Automation Server for ASPs in Delphi.</p> <p>Here is just a brief introduction to the steps.</p> <p>1. Establish an ActiveX Library Project</p> <p>Select Menu File = "New ... in Delphi, select" ActiveX Library "in the" New Item "dialog box, Delphi automatically creates a DLL project Project1.2, establish an Automation component</p> <p>Select Menu File = "New ... in Delphi, select" Automation Object "in the" New Item "dialog box; then enter Class Name in the" Automation Object Wizard "dialog box (" UPLOADFILE ") INSTANCING Select "Multiple Instance", click "OK", Delphi will automatically create a TLB (Type library) file project1_tlb.pas and a PAS (Unit) file unit1.pas. In the Type Library Design window, the project1 is named myUPload, then the file upload component's OLE registration code is "Myupload.uploadFile".</p> <p>3, introduce the ASP type library</p> <p>In order to use the five built-in objects of ASP (Request, Response, Server, Application, Session, you need to introduce the ASP type library. We mainly read data from the client to the server using the Request object.</p> <p>Select "Import Type Library" in the Project menu, select "Microsoft Active Server Pages Object Library (Version 2.0)" list in the Import Type Library dialog box (if there is no such option, please make sure your computer Install IIS3 or more or more PWS4 or ASP.dll is registered correctly), Delphi automatically creates a TLB file asptypelibrary_tlb.pas, where we need ASP object type declaration.</p> <p>4. Define onStartPage, OneNDPage Process</p> <p>When you create an OLE object instance with Server.CreateObject on the ASP page, the web server calls its method onstartpage, passes the ASP application environment information to the object, we can get client information during the process; when in the ASP page When an OLE object instance is released, the web server calls its method OneNDPage, which we can perform the end of the operation in the process. In our component, we have to use its onStartPage method. The onStartPage method should be defined in Unit1.Pas, and the function prototype of onStartPage is:</p> <p>Procedure OnStartPage (AscriptingContext: iUnknown);</p> <p>The parameter AscriptingContext is an iscriptingContext type variable that contains five attributes (Request, Response, Server, Application, Session) corresponding to the five built-name objects of the ASP.</p> <p>We need to add methods on the TLB definition window (View = "Type Library) to add iUPloadFile to" Procedure OnstartPage (AscriptingContext: iunknown); ".</p> <p>(2) Extract data uploaded by the client</p> <p>This work can be made during the onStartPage process.</p> <p>The property TOTALBYTES (request information length) and method BinaryRead can read the request information data uploaded by the client to an array of BYTE types, and then press the data format defined by RFC1867 standard. To analyze and extract data.</p> <p>1, first define several private variables of TuploadFile</p> <p>Add a reference to AsptyPelibrary_TLB.PAS in unit file UP01.PAS (saved by Unit1.Pas),</p> <p>Then join</p> <p>Private</p> <p>FcontentLength: longint; / / Request the length of the information</p> <p>FcontentData: Variant; // Content data, store request information in array form</p> <p>FFileName, // The name of the file to be uploaded</p> <p>FDELIMETER: STRING; / / Table single domain division</p> <p>FscriptingContext: iscriptingContext; // ASP processing context environment content</p> <p>FFileDataStart, // File Data Start Location</p> <p>FFileDataEnd: longint; // File data end position</p> <p>2. Extract the request information data uploaded by the client</p> <p>/ / In onStartPage event, obtain the ASP context information, request the information content, the detail of the form field, file data</p> <p>Procedure TuploadFile.onStartPage (AscriptingContext: iUnknown);</p> <p>VAR</p> <p>Arequest: IREQUEST; / / WWW Request Object</p> <p>Aolevariant: olevariant; // Record request information length</p> <p>INTDELIMTERLENGTH: INTEGER; // Division length</p> <p>LongIndex, Alongint, longpos: longint; contentdata: Ansistring; // Request information content string representation</p> <p>Strtemp: string;</p> <p>FindendoffileData: boolean; / / Whether to find the file data end position</p> <p>Begin</p> <p>/ / Extract request information data uploaded by the client</p> <p>FscriptingContext: = AscriptingContext ascriptionContext; // Get ASP Context Information</p> <p>Arequest: = fscriptingContext.Request; // Get WWW request information</p> <p>FcontentLength: = aquest.totalbytes; // Request the content length</p> <p>// Create a dynamic array for storage request information in an array form</p> <p>FconTentData: = VararrayCreate ([0, FcontentLength], Varbyte);</p> <p>// Request information content stored in an array</p> <p>Aolevariant: = fcontentLength;</p> <p>FcontentData: = areaquest.binaryRead (Aolevariant); // Read request information content</p> <p>// Translate the request information into a string, easy to position</p> <p>ContentData: = '';</p> <p>For longindex: = 0 TO FCONTENTLENGTH - 1 DO</p> <p>Begin</p> <p>ContentData: = ContentData CHR (Byte (FcontentData [longIndex]));</p> <p>IF fcontentdata [longindex] = 0 THEN BREAK; // 0 indicates the end of the content</p> <p>END;</p> <p>3, get the delimiter, upload file name</p> <p>/ / Get the delimiter of the form field</p> <p>Longpos: = POS (# 13 # 10, contentdata); // Enter the return line</p> <p>FDELIMETER: = COPY (ContentData, 1, Longpos-1); // The content before this location is a separator</p> <p>/ / Get the file name of the source path, in the request information content, file name</p> <p>// filename = "path / filename" storage</p> <p>Strtemp: = 'filename = "; // file name after" filename = "</p> <p>Longpos: = POS (strTemp, ContentData); // Get "FileName =" "location</p> <p>IF longpos <= 0 THEN</p> <p>Begin</p> <p>FFileName: = '';</p> <p>FFileDataStart: = -1;</p> <p>FFileDataEnd: = -2;</p> <p>EXIT;</p> <p>END;</p> <p>// Get the content before the next double quotation number "" ", that is, the file name of the belt source path</p> <p>Longpos: = longpos length (strTemp);</p> <p>Strtemp: = '';</p> <p>For longindex: = longpos to fcontentLength - 1 DO</p> <p>IF ContentData [longindex] <> '"' Then</p> <p>Strtemp: = strTemp ContentData [longIndex]</p> <p>Else Break;</p> <p>FFileName: = strTemp;</p> <p>4. Get the beginning of the request information content in request information, end position</p> <p>// Document data start position After the file name # 13 # 10 # 13 # 10 after DELETE (ContentData, 1, LongIndex);</p> <p>Strtemp: = # 13 # 10 # 13 # 10;</p> <p>FFileDataStart: = LongIndex POS (Strtemp, ContentData) Length (Strtemp) - 1;</p> <p>// File data end position before the next # 13 # 10 and the delimiter</p> <p>/ / Since the file data may contain illegal characters, it is no longer necessary to use a string positioning function POS.</p> <p>/ / Find the location of the next delimiter</p> <p>FFileDataEnd: = ffiledatath;</p> <p>INTDEDELIMTERLENGTH: = Length (fdelime);</p> <p>FindendoffileData: = false;</p> <p>While FfileDataEnd <= fcontentLength - INTDELIMTERLENGTH DO</p> <p>Begin</p> <p>FindendoffileData: = true;</p> <p>For alongint: = 0 to intDelimterLength - 1 DO</p> <p>IF BYTE (FDELIMETER [Alongint 1]) <></p> <p>FcontentData [ffiledataend alongint] THEN</p> <p>Begin</p> <p>FindendoffileData: = false;</p> <p>Break;</p> <p>END;</p> <p>IF FindendoffileData.</p> <p>FFileDataEnd: = ffiledataend 1;</p> <p>END;</p> <p>If not findndoffiledata kilns: = ffileDataStart - 1 // Dividend</p> <p>Else FFileDataEnd: = ffileDataEnd - 3; // Dividend, skipward # 13 # 10</p> <p>END;</p> <p>(3) Deliver information to the ASP program</p> <p>After performing (b), our uploading components can pass data to them according to the requirements of the ASP program. The data currently available is: the client source file name (FFileName, a path), file size (FFileDataEnd-ffileDataStart 1).</p> <p>First, you should declare the following two methods getFileName and getFileSize in the TLB design window.</p> <p>1. Return to the client source file name (including path)</p> <p>/ / Return to the client source file name (including path)</p> <p>Function TuploadFile.GetFileName: Olevariant;</p> <p>Begin</p> <p>Result: = ffilename; // Client Source File Name (with path)</p> <p>END;</p> <p>2, return file size</p> <p>// Return to the file size (bytes)</p> <p>Function TuploadFile.GetFileSize: olevariant;</p> <p>Begin</p> <p>Result: = ffileDataEnd - FFileDataStart 1;</p> <p>END;</p> <p>(4) Save the file</p> <p>After the operation of (b), our uploading components can save files according to the requirements of the ASP program. First of all</p> <p>Declarations in the TLB design window Savefileas and Savefiles are declared as follows. 1. Save the file by specifying the file name</p> <p>/ / Save the file by the specified file name, the parameter filename is the name of the specified file, the return value true indicates that the file is saved.</p> <p>Function TuploadFile.savefileas (filename: olevariant): olevariant;</p> <p>VAR</p> <p>LonginDex: longint;</p> <p>Afile: file of byte; // Save file in binary form</p> <p>Bytedata: byte;</p> <p>Begin</p> <p>RESULT: = TRUE;</p> <p>Try</p> <p>Assign (Afile, FileName);</p> <p>Rewrite (Afile);</p> <p>For longindex: = ffiledatastart to ffiledatand do</p> <p>Begin</p> <p>Bytedata: = byte (fcontentData [longIndex]);</p> <p>Write (Afile, ByTedata);</p> <p>END;</p> <p>Closefile (AFILE);</p> <p>Except</p> <p>Result: = FALSE;</p> <p>END;</p> <p>END;</p> <p>2, save the file by default file name</p> <p>/ / Save the file by the default file name, save the file with the same name file in the Call page</p> <p>Function TuploadFile.savefile: olevariant;</p> <p>VAR</p> <p>CurrentFilePath: String;</p> <p>Begin</p> <p>// Get the directory where the call page is located</p> <p>CurrentFilePath: = fscriptingContext.Request.servervariables ['Path_Translated'];</p> <p>CurrentFilePath: = ExtractFilePath (CURRENTFILEPATH);</p> <p>//save document</p> <p>Result: = Savefileas (CurrentFilePath ExtractFileName (FFileName));</p> <p>END;</p> <p>Third, the application of the assembly application</p> <p>In our example, Delphiup.htm is the file upload interface, and Delphiup.asp is used to perform file upload operations.</p> <p>The code of Delphiup.asp is as follows:</p> <p><! - Delphiup.asp: File upload processing page -></p> <p><html> <head> <title> file upload </ title> </ head> <body></p> <p><% DIM UPLOAD, FileName</p> <p>SET UPLOAD = Server.createObject ("Myupload.UPloadFile")</p> <p>Filename = UPLOAD.GETFILENAME</p> <p>Response.write "<br>" "& FileName &" ... "</p> <p>IF upload.savefile kil</p> <p>Response.write "<br>" "& FileName &" uploads success. "</p> <p>Response.write "<br> file size is" & upload.getfilesize & "byte."</p> <p>Else</p> <p>Response.write "<br>" "& FileName &" upload failed. "</p> <p>END IF</p> <p>SET UPLOAD = NOTHING></p> <p></ body> </ html> four, a few points</p> <p>1. The DLL file size generated by the source code automatically generated by Delphi has 215K, which can be</p> <p>In the interface segment of asptypelibrary_tlb.pas, the units in the USES are removed all in ActiveX, in</p> <p>All units in USES are removed in Myupload_TLB.PAS, and the generated DLL file size can be reduced to 61K.</p> <p>2. The above method is equally applicable to the CGI program, but use the TwebRequest object.</p> <p>The above programs are pronounced in PWIN98 Delphi3.0 PWS4.0.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-56885.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="56885" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.085</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '4ONPHMp1UT2j8Fzw1zyjhRspYUtSbnOjauuEi3LKyKkNuJKIE0wBsYbU4MHGAReWpWAiNs6A6IOV2uQ_2Bf3o4OQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>