Use Delphi to develop file upload components for ASP

zhaozj2021-02-08  250

Uploading components for ASP development files Upload components Shenzhen Wangfajun returns ASP (Active Server Page) is Microsoft's products, because it is easy to play, can quickly develop functional dynamic sites, now many websites (especially intranet / extranet The model of NT IIS ASP has used the ASP to become a very popular 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 implementation principle of file upload is based on web mode data, to follow the RFC1867 standard, the uploaded file data is no exception.

If you use the following HTML page file (Delphiup.htm) to load file: file Upload </ title> </ head> <body> > File Upload Components Writing with Delphi Implementation File Upload <form name = "UploadForm" action = "delphiup.asp" method = "post" enctype = "multipart / form-data"> <p> file saved as: <input Type = text name = "saveas"> <p> To select the file: <input type = file name = "fileData"> <input type = "submit" Name = "b1" value = "Confirm Upload"> </ P> </ form> </ body> </ html> When the client selects a file (such as Test.txt, its content is "here is a content for uploading files.") and press "Confirm Upload" After the button is submitted, the data received by the server server will have the following form: ----------------------------- 7cf1d6c47c # 13 # 13 # 13 # 13 # 13 # 13 # 13 # 13 # 13 10content-disposition: form-data; name = "saveas" # 13 # 10 # 13 # 10NewFileName # 13 # 10 ------------------------- ---- 7CF1D6C47C # 13 # 10content-disposition: form-data; name = "fileData"; filename = "d: /test.txt" Content-Type: text / place # 13 # 10 # 13 # 10 Here is a The content used to upload the file. # 13 # 10 ----------------------------- 7CF1D6C47C # 13 # 10content-disposition: form-data; name = "b1" # 13 # 10 # 13 # 10 Confirm to upload # 13 # 10 ---------------------------- 7CF1D6C47C - Among them, " -------------------------- 7CF1D6C47C "is a delimiter, used for individual domains in the form (FORM); # 13 # 10 It 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.</p> <p>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. Second, the implementation process of file uploads After understanding the data format mentioned above, it is no difficult to write a file to upload components. (1) Beginning to establish an ASP component If you are not familiar with the steps of developing Ole Automation Server with Delphi, see "An article in the 06th issue of Electronics and Computer", "developed with Delphi for ASP Ole Automation Server. Here is just a brief introduction to the steps. 1. Establish an ActiveX Library Project Select Menu File = "New ... in Delphi, select" ActiveX Library "in the" New Item "dialog box, Delphi will automatically create a DLL project Project1.2, create an Automation Components 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 (such as" UploadFile " ), Instancing selects "Multiple Instance", click "OK", Delphi automatically creates 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". 3. Introducing the ASP Type Library In order to use the five built-in objects of the 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. 4. Define onStartPage, OneNDPage Process 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 the client in the process. Information; When an OLE object instance is released in the ASP page, the web server calls its method OneendPage, which we can perform the end of the operation in the process. In our component, we have to use its onStartPage method. OnStartPage method should be defined in the Unit1.PAS, OnStartPage function prototype: procedure OnStartPage (AScriptingContext: IUnknown); AScriptingContext wherein the parameter is a variable IScriptingContext type, comprising five attributes (Request, Response, Server, Application, Session) respectively corresponding to The five built-name objects of the ASP. We need to add methods on the TLB definition window (View = "Type Library) to add iUPloadFile to" Procedure OnstartPage (AscriptingContext: iunknown); ". (2) The data on which the client uploads uploaded can be made in the onStartPage process. 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. Firstly, several private variables of TuploadFile are added to the unit file UP01.PAS (Saves from Unit1.Pas) to the reference (Uses) of AsPTyPelibrary_TLB.PAS, then join privatefcontentLength: longint; // request information length fcontentData: Variant ; // Content data, store request information in an array form FFileName, // The file name to upload FDELIMETER: String; // Table Single Division FScriptingContext: iscriptingContext; // ASP Processing Context Environment FFileDataStart, // File Data Start Location FFileDataEnd: longint; // File Data Attach 2 AScriptingContext: IUnknown); varARequest: IRequest; // WWW request object AOleVariant: OleVariant; // record request content length intDelimterLength: integer; // delimiter length longIndex, ALongInt, longPos: LongInt; contentData: AnsiString; // request information a string representation of the contents strTemp: string; FindEndOfFileData: boolean; // Did you find the file data end position begin // extract client data upload request information FScriptingContext: = AScriptingContext as IScriptingContext; // get context information ASP ARequest: = FScriptingContext. Request; // Get the WWW request information fcontentLength: = allquest.totalbytes; // Request information length // Create a dynamic array for storage request information in an array form: = var., Varbyte; // Request information content to array in array Evariant: = fcontentLength; fcontentdata: = allquest.binaryread (Aolevariant); // Read request information content // Transforms the request information to a string, easy to position CONTENTDATA: = ''; for longindex: = 0 to fcontentLength - 1 DOBEGINCONTENTDATA: = ContentData CHR (Byte (fcontetdata [longindex])); if fcontentdata [longindex] = 0 THEN BREAK; // 0 Indicates content ending END; 3, get the delimiter, upload file name // Get the boundary of the form field LONGPOS: = POS (# 13 # 10, contentdata); // Enter the return line in the position fdelimeter: = COPY (ContentData, 1, longpos-1); // The content before this location is a separator // Get tape The file name of the source path is stored in the request information content, the file name is stored in the form of // filename = "path / filename" forms straw: = 'filename = "; // file name after" FileName = ", Longpos: =</p> <p>POS (strTemp, contentdata); // Get "filename =" "position if longpos <= 0 dameginffilename: = '; ffileDataStart: = -1; ffiledatand: = -2; exit; end; // Get the next double quotation number "" "" Previous content, the file name of the belt source path "longpos: = longpos length (strTemp); strTemp: = '; for longindex: = longpos to fcontentLength - 1 doif ContentData [longindex] <>'" 'ThenStemp : = strTemp ContentData [longIndex] else break; ffilename: = strTemp; 4 # 13 # 10 after Delete (ContentData, 1, longindex); strTemp: = # 13 # 10 # 13 # 10; ffileDataStart: = longindex pos (strTemp, ContentData) length (strTemp) - 1; // file data end Location is in the next # 13 # 10 and the delimiter // Since the file data may contain illegal characters, it is no longer able to use the string positioning function POS // to find the next division of the position ffiledataEnd: = ffileDataStart; INTDELIMTERLENGTH: = Length (fdelimeter) ; FindEndOfFileData: = false; while FFileDataEnd <= FContentLength - intDelimterLength dobeginFindEndOfFileData: = true; for ALongInt: = 0 to intDelimterLength - 1 doif Byte (FDelimeter [ALongInt 1]) <> FContentData [FFileDataEnd ALongInt] thenbeginFindEndOfFileData: = false ; Break; end; if FindEndOfFileData then break; FFileDataEnd: = FFileDataEnd 1; end; if not FindEndOfFileData then FFileDataEnd: = FFileDataStart - 1 // else FFileDataEnd delimiter not found: = FFileDataEnd - 3; // delimiter forward Skip # 13 # 10nd; (3) Passing information to the ASP program After performing (b), our uploading components can pass data 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). 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) // Return to the client source file name (including path) Function TuploadFile.GetFileName: olevariant; beginResult: = ffilename; // Client source file name (including path) end; 2 Back to file size // Return file size (Bytes) function tuploadFile.GetFileSize: olevariant; beginResult: = ffileDataEnd - ffileDataStart 1; End; (4) Save file After the (2) operation, our uploading components can The file is saved according to the requirements of the ASP program. The following two methods Savefileas and Savefile should be declared in the TLB design window. 1. Press the specified file name Save the file // Press the specified file name to save the file, the parameter filename is the specified file name, the return value true indicates that the file saves function function tuploadFile.savefileas (filename: olevariant): Olevariant; VarlongIndex: longint; Afile : file of byte; // save the file in binary form byteData: byte; beginresult: = true; tryassign (aFile, FileName); rewrite (aFile); for longIndex: = FFileDataStart to FFileDataEnd dobeginbyteData: = byte (FContentData [longIndex] Write (AFILE, BYTEDATA); End; Closefile (AFILE); EXCEPTRESULT: = false; end; end; 2, save files by default file name // Press the default file name to save the file, save the file with the same name file Save in the call to the directory where the page function TUploadFile.SaveFile: OleVariant; varCurrentFilePath: string; begin // gets called page directory CurrentFilePath: = FScriptingContext.Request.ServerVariables [ 'PATH_TRANSLATED']; CurrentFilePath: = ExtractFilePath (CurrentFilePath); // save the file Result: = Savefileas (CURRENTFILEPATH ExtractFileName (FFileName)); END; 3, Uploading Components Application Example In our example, Delphiup.htm is the file upload interface, Delphiup.asp is used to perform file upload operations.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-1603.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="1603" 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.048</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 = 'h7MSDKgoWWHKHdNW2vKtbKjnGwX8NSiNYdSOSYEANidmJH4ie68hfL1GMDcoJn6s_2FXzckYZCHOXAuzu5Fp3csQ_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>