XML technology upload file

xiaoxiao2021-03-06  22

XML technology upload file

Type: XML / BizTalk

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:

Create an XML document using MSXML 3.0

Create an XML node for binary content

Use ADO Stream Object to put uploaded file data into the node

Send this XML document to the web server using the XMLHTTP object

Service-Terminal:

Read the data in the XML document reading the data in the binary node from the Request object and stored in 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. The 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></p> <p><Body></p> <p><Input ID = btn_send name = "btn_send" type = button value = "file send"></p> <p><Div id = div_message> Ready</p> <p></ Body></p> <p></ Html></p> <p><Script language = javaScript></p> <p>// upload function</p> <p>Function BTN_SEND.ONCLICK ()</p> <p>{</p> <p>// Create an ADO-stream object</p> <p>VAR ado_stream = new activXObject ("adodb.stream");</p> <p>// Create an XML document containing the default header information and root node</p> <p>VAR XML_DOM = New ActiveXObject ("msxml2.domdocument");</p> <p>XML_DOM.LOADXML ('');</p> <p>/ / Specify data type</p> <p>XML_Dom.documentelement.setttribute ("XMLns: DT", "URN: Schemas-Microsoft-COM: DataTypes");</p> <p>// Create a new node and set it as a binary data node</p> <p>Var l_node1 = XML_Dom.createElement ("file1"); l_node1.datatype = "bin.base64";</p> <p>// Open the stream object, read the source file</p> <p>ADO_STREAM.TYPE = 1; // 1 = adtypebinary</p> <p>ADO_STREAM.Open ();</p> <p>ADO_STREAM.LOADFROMFILE ("c: //tmp//myfile.doc");</p> <p>// store the file content into the XML node</p> <p>L_node1.nodetypedValue = ado_stream.read (-1); // -1 = adreadall</p> <p>ADO_STREAM.CLOSE ();</p> <p>XML_Dom.documentelement.Appendchild (l_node1);</p> <p>/ / You can create multiple binary nodes, upload multiple files at a time</p> <p>// Send the XML document to the web server</p> <p>VAR XMLHTTP = New ActiveXObject ("Microsoft.xmlhttp");</p> <p>XMLHTTP.Open ("post", "./ file_recieve.asp", false;</p> <p>Xmlhttp.send (XML_DOM);</p> <p>/ / Display the information returned by the server</p> <p>Div_message.innerhtml = xmlhttp.responsetext;</p> <p>}</p> <p></ Script></p> <p>The following code uses the same object to provide the server-side upload processing function.</p> <p><% @ Language = VBScript%></p> <p><% OPTION Explicit</p> <p>Response.expires = 0</p> <p>'Define variables and objects.</p> <p>DIM ADO_STREAM</p> <p>DIM XML_DOM</p> <p>DIM XML_FILE1</p> <p>'Creating a stream object</p> <p>Set ado_stream = server.createObject ("adoDb.stream")</p> <p>'Creating an XMLDOM object from the Request object</p> <p>SET XML_DOM = Server.createObject ("msxml2.domdocument")</p> <p>XML_DOM.LOAD (Request)</p> <p>'Reads nodes containing binary data</p> <p>SET XML_FILE1 = XML_Dom.selectsinglenode ("root / file1")</p> <p>'Open the Stream object and store the data in it.</p> <p>ADO_STREAM.TYPE = 1 '1 = adtypebinary</p> <p>ADO_STREAM.Open</p> <p>ADO_STREAM.WRITE XML_FILE1.NODETYPEDVALUE</p> <p>'File storage</p> <p>ADO_STREAM.SAVETOFILE "C: /TMP/UPLOAD1.DOC", 2 '2 = AdsavecreateOverWrite</p> <p>ADO_STREAM.CLOSE</p> <p>'Destroying object</p> <p>Set ado_stream = Nothing</p> <p>SET XML_DOM = Nothing</p> <p>'Return to your browser</p> <p>Response.write "Upload Successful!"</p> <p>%></p> <p>You can also use the Stream object to place the data into the BLOB field of the database.</p> <p>Benefits to use this method</p> <p>Does not cause page conversion.</p> <p>No special components are required.</p> <p>Multiple files can be uploaded simultaneously.</p> <p>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.</p> <p>System security consideration</p> <p>This method can only be used in the internal network because it requires the security level of IE5 to be "low". Must: allow scripts and ActiveX objects. This setting allows the browser to perform a JScript statement similar to "myobj = new activityXObject (...);</p> <p>The data source must be allowed to pass through the domain. 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.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-43288.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="43288" 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.211</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 = 'WfY7tNMcFRAB8PvHXxXtjbd86080ci0NjPRZhkD3tbCO3sqv19TgIi9Umrtt4eHQ7ap37DccxpCBX1BI_2BnEPCg_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>