XML technology upload file-paste

Source: Internet
Author: User
Tags base64 file system file upload implement client
xml| upload XML technology upload file

Overview
This article explains an example of uploading files using XML technology, and there are no limitations in the traditional approach. This example describes how to use the MSXML3.0 and ADO stream objects to implement this new upload method. There are a lot of benefits, for example, not needing a dedicated upload component.


Introduction
In order to obtain the upload function in the HTML webpage, we can use the form in the following format in the client:

<form name= "MyForm"
action= "Targeturl.asp"
Enctype= "Multipart/form-data"
Method= "POST" >
<input type= "File" name= "MyFile" >
<input type= "Submit" value= "Upload File" >
</FORM>

This scenario has many limitations on both the client and server side. First, we must use the Post method, because the Get method cannot process such form data. Also, there is no way to raise a post action without using the form. Once the data is sent to the form handler, the browser will load the handler as a new page, and the user will see a page conversion process that is not pleasing to the person.
The Enctype property defines MIME encoding for the form, and the Enctype property of the form that uploads the file must use "Multipart/form-data". Setting this property to "Multipart/form-data" creates a post buffer (composite structure) that differs from the traditional structure, and the ASP's request object cannot access such form content. So we can use the Request.BinaryRead method to access this data, but we can't do it with scripting language. The Request.BinaryRead method returns a Vtarray type of data (a Variant array containing only unsigned byte characters). However, the scripting language can only handle variant types of data. To solve this problem, you can only use a dedicated ASP upload component, or an ISAPI extender, such as CPSHOST.DLL. This is a design limitation.

New upload Scheme

You need to follow the steps below.
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 this node
Use the XMLHTTP object to send this XML document to the Web server

Server side:
To read an XML document from the Request object
Reads the data from a binary node and stores it in a file on the server. Of course, we can also store it in a blob-type field in the database.
Before we explain this code, we can do some thinking about the program.

The thinking of XML

XML Formats support many types of data, such as numeric, float, character, and so on. Many authors define XML as ASCII format, but we cannot ignore that XML technology can also use "bin.base64" data types to describe binary information. This feature is fully supported in Ms XML3.0 resolution, but some special settings are still required. This object provides a number of properties that can be fully controlled for binary data:

Obj_node.datatype-This 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 Read-write property contains data for the specified node, as defined by the type of development.
We can create an XML document that contains multiple bin.base64 types of nodes that contain uploaded files. This feature allows you to upload multiple files at once with a post.

We can send an XML document to the Web server using the XMLHttpRequest object and post method. This object provides client-side protocol support for HTTP servers, allowing the sending and receiving of Ms Xmldom objects on the Web server. XMLHttpRequest is a COM object built into Internet Explorer 5 (no custom installation is required) and does not need to convert the page after it has been sent.


Thoughts on the object of ADO stream

We can create an XML document that contains one or more binary nodes on the client. We must also fill in the contents of the file into the node. Unfortunately, the scripting language does not have access to the local file system, and the Scripting.filesystem object (the built-in object of the WIN32 system) has so far not been able to access the binaries. This is a design limitation. So we need to find another COM object that can provide access to a local binary file.

The ADO Stream object (the component in MDAC 2.5) provides the means to read, write, and manage binary stream data. The contents of a byte stream can be text or binary data, and there is no capacity limit. In ADO 2.5, Microsoft's introduction of the stream object does not belong to any layer of the ADO object structure, so we can use it without bundling.

The stream object is used in this article to access the contents of the file, and then the content is stored in an XML node.


Client

The following example code completes the file upload action using the stream and MSXML objects.

<HTML>
<BODY>
<input id=btn_send name= "btn_send" Type=button value= "FILE send" >
<div id=div_message>ready</div>
</BODY>
</HTML>

<script language=javascript>

Upload function
function Btn_send.onclick ()
{
Creating Ado-stream Objects
var ado_stream = new ActiveXObject ("ADODB. Stream ");

Create an XML document that contains the default header information and the root node
var xml_dom = new ActiveXObject ("MSXML2. DOMDocument ");
Xml_dom.loadxml (' <?xml version= ' 1.0 "?> <root/>");
Specifying data types
Xml_dom.documentElement.setAttribute ("Xmlns:dt", "urn:schemas-microsoft-com:datatypes");

Create a new node and set it as a binary data node
var l_node1 = xml_dom.createelement ("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");
Storing the contents of a file in an XML node
L_node1.nodetypedvalue = Ado_stream. Read (-1); -1=adreadall
Ado_stream. Close ();
Xml_dom.documentElement.appendChild (L_NODE1);

Can create multiple binary nodes, upload multiple files at a time

To send an XML document to a Web server
var xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
Xmlhttp.open ("POST", "./file_recieve.asp", false);
Xmlhttp.send (Xml_dom);
Displaying information returned by the server
div_message.innerhtml = xmlhttp. ResponseText;
}
</SCRIPT>


Server-side

The following code uses the same object to provide server-side upload processing capabilities.

<%@ language=vbscript%>
<% Option Explicit
Response.Expires = 0

' Define variables and objects.
Dim Ado_stream
Dim xml_dom
Dim xml_file1

' Create a Stream object
Set ado_stream = Server.CreateObject ("ADODB. Stream ")
' Create a Xmldom object from the Request object
Set xml_dom = Server.CreateObject ("MSXML2. DOMDocument ")
Xml_dom.load (Request)
' Read the node that contains the binary data
Set xml_file1 = Xml_dom.selectsinglenode ("Root/file1")

' Open the Stream object and deposit the data in it
Ado_stream. Type = 1 ' 1=adtypebinary
Ado_stream.open
Ado_stream. Write Xml_file1.nodetypedvalue
' File disk
Ado_stream. SaveToFile "C:\tmp\upload1.doc", 2 ' 2=adsavecreateoverwrite
Ado_stream.close

' Destroy objects
Set Ado_stream = Nothing
Set xml_dom = Nothing
' Return information to the browser
Response.Write "Upload successful!"
%>

You can also use the stream object to place the data in a BLOB field in the database.

Benefits of using this method

does not cause page conversions.
No private components are required.
Multiple files can be uploaded at the same time.
This program is written in plain script and can be easily inserted into other code without the need for any matching HTML objects. You can also implement this logic in any language that supports COM standards.

System Security Considerations

This method can only be used for the internal network because it requires the IE5 security level to be set to low. Have to:

Allow scripts and ActiveX objects. This setting allows the browser to perform a similar "myobj = new ActiveXObject (...)" JScript statement;
You must allow access to the data source across domains. 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 both the server and the client.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.