Use XML to help upload files

zhaozj2021-02-16  48

Marco nanni

Overview

This paper explains an example of using XML technology to upload files, using this method without all kinds of limits in the conventional method. This example tells how this new upload method is implemented using MSXML3.0 and ADO Stream objects. There are many advantages, for example, there is no need for dedicated upload components.

introduction

To get an upload function in an HTML page, we can use the format in the client:

This solution has many restrictions on the use of the client and server. First, we must use the POST method because the GET method cannot handle such form data. Also, there is no way to trigger a POST action without using a form. After sending data to the form handler, the browser will load the handler as a new page, and then the user will see a page conversion process that does not expect. ENCTYPE Attribute Defines the MIME encoding method for the form. The encType property of the form of the upload file must use "Multipart / Form-Data". Set this property to "Multipart / Form-Data" created a POST buffer (composite structure) different from the traditional structure, the ASP's Request object cannot access such form content. So, we can use the Request.BinaryRead method to access these data, but you can't use the scripting language to complete all. Request.binaryRead method Returns a VTARRAY type data (only a Variant type array of unmanned byte characters). But the scripting language can only handle Variant data. To solve this problem, you can only use a dedicated ASP upload component, or the ISAPI extension, such as cpshost.dll. This is a design restriction. New uploading plan

The following steps need to be followed. Client:

Creating an XML Document using MSXML 3.0 Creating an XML node for binary content Using ADO Stream Object Add Uploaded file data into the node Using XMLHTTP objects to send this XML document to the web server

Server side: Reads the data in the XML document reads the data in the binary node from the Request object and stores the file on the server. Of course, we can also store them into the BLOB field of the database. Before explaining this code, we can think about this program. Think about XML

XML format supports many data types, such as Numeric, Float, Character, and more. Many authors define XML as ASCII formats, but we cannot ignore, XML technology can also use the "bin.base64" data type to describe binary information. This feature is fully supported in the MS XML3.0 parser, but some special settings are required. This object provides some properties that can be fully controlled to binary data:

Obj_node.DataType - This readable and writable property defines the data type of a particular node. MSXML parser supports more data types (see msdn: http://msdn.microsoft.com/library/psdk/xmlsdk/xmls3z1v.htm)

For binary data, we can use the "bin.base64" type. OBJ_Node.NodeTypedValue - The readable write property contains data in specified nodes represented by the setting type.

We can create an XML document containing multiple bin.base64 type nodes, which contains uploaded files. This feature can use a POST upload multiple files at a time.

We can send an XML document to the web server using the XMLHttpRequest object and the POST method. This object provides client protocol support for HTTP servers, allowing MS XMLDOM objects to be sent and accepted on a web server. XMLHttpRequest is a COM object built into Internet Explorer 5 (do not need to be customized), and no conversion page is required after sending.

Thoughts on the ADO stream object

We can create an XML document containing one or more binary nodes on the client. We must also fill the file content into the node. But unfortunately, the scripting language cannot access the local file system, and the scripting.FileSystem object (the built-in object of the Win32 system) is not available so far. This is a design restriction. So we need to find a COM object that can provide access to local binary files.

ADO Stream objects (components in MDAC 2.5) provide means for reading, writing, and managing binary flow data. The content of the byte stream can be text, or binary data, and there is no limit on capacity. In ADO 2.5, Microsoft's introduction to the Stream object is not any layer of the ADO object structure, so we can use this object without bundling.

This article uses the Stream object to access the file content, and then store the content into the XML node.

Client

The following sample code completes the file upload action using the Stream and MSXML objects.

file send </ title> </ head> <body> <input id = btn_send name = "btn_send" type = button value = "file send"> <div id = div_message> Ready < / DIV> </ body> </ html></p> <p><Script language = javaScript></p> <p>// Upmit function function btn_send.onclick () {// Create ADO_STREAM Object VAR ADO_STREAM = New ActiveXObject ("AdoDb.Stream");</p> <p>// Create an XML document containing the default header information and root node VAR XML_DOM = New ActiveXObject ("msxml2.domdocument"); XML_DOM.LOADXML ('<? XML Version = "1.0"?> <Root />'); // Specify data type XML_DOM.Documentelement.setttribute ("XMLns: DT", "URN: Schemas-Microsoft-COM: DataTypes");</p> <p>// Create a new node, set it as a binary data node var l_node1 = XML_DOM.CREATEEEEEEMENT ("file1"); l_node1.datatype = "bin.base64"; // Open the stream object, read source file ADO_STREAM.TYPE = 1; // 1 = adtypebinary ado_stream.open (); ado_stream.loadFromfile ("c: //tmp//myfile.doc"); // Put the file content into the XML node l_node1.nodetyped (-1); // -1 = adreadAll ado_stream.close (); XML_DOM.Documentelement.Appendchild (l_node1); // You can create multiple binary nodes, upload multiple files at a time</p> <p>// Send the XML document to the web server var xmlhttp = new activityXObject ("microsoft.xmlhttp"); XMLHttp.Open ("post", "./ file_recieve.asp", false; xmlhttp.send; ///// Display the information returned by the server div_message.innerhtml = xmlhttp.responsetext;} </ script></p> <p>Service-Terminal</p> <p>The following code uses the same object to provide the server-side upload processing function.</p> <p><% @ Language = vbscript%> <% OPTION Explicit Response.expires = 0 'Defines variables and objects. DIM ADO_STREAM DIM XML_DOM DIM XML_FILE1</p> <p>'Create a Stream object set ado_stream = Server.CreateObject ( "ADODB.Stream")' Create Object Request XMLDOM object from the set xml_dom = Server.CreateObject ( "MSXML2.DOMDocument") xml_dom.load (request) 'reads out the binary data comprising Node SET XML_FILE1 = XML_Dom.selectsinglenode ("root / file1")</p> <p>'Open Stream object, wherein the data into ado_stream.Type = 1' 1 = adTypeBinary ado_stream.open ado_stream.Write xml_file1.nodeTypedValue 'File Save ado_stream.SaveToFile "c: /tmp/upload1.doc", 2' 2 = adSaveCreateOverWrite ADO_STREAM.CLOSE</p> <p>'Destroy the object set ado_stream = Nothing set XML_DOM = Nothing' Return to the browser Response.write "Upload Successful!"%></p> <p>You can also use the Stream object to place the data into the BLOB field of the database. Benefits to use this method</p> <p>Does not cause page conversion. No special components are required. Multiple files can be uploaded simultaneously. This program is written in pure script, which can be easily inserted into other code without the matching of any HTML object. This logic can also be implemented in any language that supports the COM standard. System Security Considering that this method can only be used for internal networks because it requires IE5 security levels to be "low". have to:</p> <p>Allow scripts and ActiveX objects. This setting allows the browser to perform a JScript statement similar to "MyObj = new activityXObject (...); must allow the crossover to access the data source. This setting allows the Stream object to be used on the client. MS XML DOM 3.0 and MDAC 2.5 must also be installed on the server and client. References</p> <p>Read Tiago Halm's article about traditional file-upload processing at http://www.15seconds.com/Issue/001003.htm For a description of the data types supported by MS XML parser, see http://msdn.microsoft.com/ library / psdk / xmlsdk / xmls1cbp.htm and http://msdn.microsoft.com/library/psdk/xmlsdk/xmls3z1v.htm A sample of creating an XML document with binary data in VB is available at http: // support. Microsoft.com/support/kb/articles/q254/3/88.asp for a Microsoft Reference on the ado stream object, see http://msdn.microsoft.com/library/psdk/dasdk/mdao1ajx.htm. Another Sample For Managing Blob and Binary Files Can Be Found At http://support.microsoft.com/support/kb/articles/q258/0/38.asp. About the Author</p> <p>Marco Nanni Is An Italian Web Developer with Experience In (D) HTML AND XML Technology Used for Implementing Enterprise Solutions. Marco Can Be Contacted At: mnanni@lycos.it.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-28265.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="28265" 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.051</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 = 'Guc86n4PtGwwQ0OBd5LH_2FZT_2Bcucx6m4Oc6mAaLyz4JnRmvxdYTjpAT7zU3dZ00N365Uc1ymwWNKjwbSIMepozg_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>