XML technology upload file

xiaoxiao2021-03-06  20

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. 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.nodetyped

Value - This 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 very unfortunate, the scripting language cannot access the local file system, and

Scripting.FileSystem object (the built-in object of 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 < / Body> </ html> <script language = javascript> // Upmit function function btn_send.onclick () {// Create ADO_STREAM Object VAR ADO_STREAM = New ActiveXObject ("AdoDb.Stream"); ​​// Creating the default head Information and root node XML document VAR XML_DOM = New ActiveXObject ("msxml2.domdocument"); XML_DOM.LOADXML (); // Specify data type XML_Dom.DocumentElement.setttribute ("XMLns: DT", "URN: Schemas-Microsoft- COM: DATATYPES "); // Create a new node, set it as binary data node var l_node1 = XML_DOM.CREATEEEEEMENT (" file1 "); l_node1.datatype =" bin.base64 "; // Open 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 value = ado_stream. Read (-1); // -1 = adreadall ado_stream.close (); XML_DOM.DocumentElement.AppendChild (l_node1); // You can create multiple binary nodes, upload multiple files at a time, send XML documents to the web server VAR XMLHTTP = New ActiveXObject ("Microsoft.xmlhttp"); XMLHttp.ope n ("post", "./ file_recieve.asp", false); xmlhttp.send; // Display the information returned by the server div_message.innerhtml = xmlhttp.ResponseText;} </ script> server side</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 Create a Stream object xml_file1 set ado_stream = Server.CreateObject ( "ADODB.Stream") created from the object XMLDOM Request object set xml_dom = Server.CreateObject ( "MSXML2.DOMdocument") xml_dom.load (request) comprising readout node binary data set xml_file1 = xml_dom.selectSingleNode ( "root / file1") to open the 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 Destroy object set ado_stream = nothing set XML_DOM = Nothing to the browser RESPONSE.WRITE" UPLOAD SUCCESSFUL! "%> You can also use the stream object to put the data Database's BLOB field.</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". have to:</p> <p>Allow scripts and ActiveX objects. This setting allows the browser to perform similar</p> <p>"myobj = new activ"</p> <p>"J</p> <p>SCRIPT statement;</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-44295.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="44295" 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.044</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 = 'gLlaVe3dz2E_2Fk4zqKbphRvcDjAHM0OcSxweA7Dm_2F3Vh7OivdumrwJkpRJi1NoxeCw1zKQiFtcgAB2ySX0wjLQQ_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>