


Detailed introduction to XML technology for uploading files
xml技术上传文件
类型: XML/Biztalk
概述
本文讲解了一个使用XML技术上传文件的例子,使用该方法没有传统方法中的种种限制。 这个例子讲述了如何使用MSXML3.0和ADO Stream对象来实现这种新的上传方法。好处有很多,比如,不需要专用的上传组件。
引言
为了在HTML网页中获得上传功能,在客户端我们可以使用如下格式的FORM
< FORM NAME="myForm" ACTION="TargetURL.asp" ENCTYPE="multipart/form-data"METHOD="post" > < INPUT TYPE="file" NAME="myFile" > < INPUT TYPE="submit" VALUE="Upload File" > < /FORM >
这种方案在客户端和服务器端的使用都有很多限制。首先,我们必须使用POST方法,因为GET方法无法处理这样的表单数据。并且,没有什么方法可以在不使用表单的情况下引发一个POST动作。把数据发送给表单处理程序后,浏览器将会把处理程序作为新页面加载,然后使用者会看到一个不讨人喜欢的页面转换过程。
ENCTYPE属性为表单定义了MIME编码方式,上传文件的表单的ENCTYPE属性必须使用“multipart/form-data”。把这个属性设置为“multipart/form-data”就创建了一个与传统结构不同的POST缓冲区(复合结构),ASP的Request对象无法访问这样的表单内容。所以,我们可以使用Request.binaryRead方法来访问这些数据,但是无法使用脚本语言来完成这一切。Request.binaryRead方法返回一个VTarray型数据(只包含无符号一字节字符的Variant型数组)。但是脚本语言只能处理Variant型数据。为了解决这个问题,只能使用专用的ASP上传组件,或者ISAPI扩展程序,比如CPSHOST.DLL。这是设计上的限制。
新的上传方案
需要按照如下步骤操作。
客户端:
使用MSXML 3.0创建一个XML文档
创建一个针对二进制内容的XML节点
使用ADO Stream object将上传的文件数据放入该节点
使用xmlhttp对象把这个XML文档发送给Web服务器
服务器端:
从Request对象中读出XML文档读出二进制节点中的数据并且存储到服务器上的文件中。当然,我们也可以将其存储到数据库的BLOB型字段中。
在解释这段代码之前,我们可以对这个方案进行一些思考。
对XML的思考
XML格式支持很多数据类型,比如numeric, float, character等等。很多作者将XML定义为ASCII格式,但是我们不能忽视,XML技术还可以使用“bin.base64”数据类型来描述二进制信息。这个特性在MS XML3.0解析器重得到完全的支持,但是目前还需要一些特别设置。该对象提供一些可以对二进制数据进行完全控制的属性:
obj_node.dataType - 该可读写的属性定义了特定节点的数据类型。MSXML解析器支持更多的数据类型(参见MSDN:http://msdn.microsoft.com/library/PSDk/xmlsdk/xmls3z1v.htm)
对于二进制数据,我们可以使用“bin.base64”类型。
obj_node.nodeTypedValue - 该可读写属性包含了按照制定类型表示的指定节点的数据。
我们可以创建一个包含多个bin.base64类型节点的XML文档,节点中包含上传的文件。这点特性可以使用一个POST一次上传多个文件。
我们可以使用XMLHttPRequest对象和POST方法发送一个XML文档给Web服务器。该对象为HTTP服务器提供了客户端协议支持,允许在Web服务器上发送和接受MS XMLDOM对象。XMLHttpRequest是Internet Explorer 5内置的COM对象(不需要定制安装),并且发送完毕后无需转换页面。
对ADO Stream对象的思考
我们可以在客户端创建一个包含一个或者多个二进制节点的XML文档。我们还必须把文件内容填入节点中。但是很不幸,脚本语言不能访问本地文件系统,并且Scripting.FileSystem对象(是Win32系统的内置对象)到目前为止还不能访问二进制文件。这是设计上的限制。所以我们需要另外找一个可以提供对本地二进制文件的访问的COM对象。
ADO Stream对象(MDAC 2.5中的组件)提供了读、写和管理二进制流数据的手段。字节流的内容可以是文本,或者二进制数据,并且没有容量上的限制。在ADO 2.5中,Microsoft对Stream对象的介绍不属于ADO对象结构的任何一层,所以,我们无需捆绑即可使用该对象。
本文中使用Stream对象来访问文件内容,再把内容存入XML节点。
客户端
以下示例代码使用Stream和MSXML对象完成文件上传动作。
< HTML > < HEAD >< TITLE >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 > // 上传函数 function btn_send.onclick() { // 创建 ADO-stream 对象 var ado_stream = new ActiveXObject("ADODB.Stream"); // 创建包含默认头信息和根节点的 XML文档 var xml_dom = new ActiveXObject("MSXML2.DOMDocument"); xml_dom.loadXML(' '); // 指定数据类型 xml_dom.documentElement.setAttribute("xmlns:dt", "urn:schemas-microsoft-com:datatypes"); // 创建一个新节点,设置其为二进制数据节点 var l_node1 = xml_dom.createElement("file1"); l_node1.dataType = "bin.base64"; // 打开Stream对象,读源文件 ado_stream.Type = 1; // 1=adTypeBinary ado_stream.Open(); ado_stream.LoadFromFile("c:\\tmp\\myfile.doc"); // 将文件内容存入XML节点 l_node1.nodeTypedValue = ado_stream.Read(-1); // -1=adReadAll ado_stream.Close(); xml_dom.documentElement.appendChild(l_node1); // 可以创建多个二进制节点,一次上传多个文件 // 把XML文档发送到Web服务器 var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); xmlhttp.open("POST","./file_recieve.asp",false); xmlhttp.send(xml_dom); // 显示服务器返回的信息 div_message.innerHTML = xmlhttp.ResponseText; } < /SCRIPT >
服务器端
以下代码使用相同的对象提供服务器端的上传处理功能。
< %@ LANGUAGE=VBScript% > < % Option Explicit Response.Expires = 0 ' 定义变量和对象。 dim ado_stream dim xml_dom dim xml_file1 ' 创建 Stream 对象 set ado_stream = Server.CreateObject("ADODB.Stream") ' 从Request对象创建 XMLDOM对象 set xml_dom = Server.CreateObject("MSXML2.DOMDocument") xml_dom.load(request) ' 读出包含二进制数据的节点 set xml_file1 = xml_dom.selectSingleNode("root/file1") ' 打开Stream对象,把数据存入其中 ado_stream.Type = 1 ' 1=adTypeBinary ado_stream.open ado_stream.Write xml_file1.nodeTypedValue ' 文件存盘 ado_stream.SaveToFile "c:\tmp\upload1.doc",2 ' 2=adSaveCreateOverWrite ado_stream.close ' 销毁对象 set ado_stream = Nothing set xml_dom = Nothing ' 向浏览器返回信息 Response.Write "Upload successful!" % >
也可以使用Stream对象把数据放到数据库的BLOB型字段中。
使用该方法的益处
不引起页面转换。
不需要专用组件。
可同时上传多个文件。
这段程序是纯脚本写成的,可以很容易的插入到其他代码中,而不需要任何HTML对象的配合。还可以把这个逻辑在任何支持COM标准的语言中实现。
系统安全考虑
该方法只能使用于内部网络,因为它需要IE5的安全级别设置为“低”。必须:
允许脚本和ActiveX对象。该设置允许浏览器执行类似 "myobj = new activexobject(...)"的 JScript语句;
必须允许穿越域访问数据源。这个设置允许在客户端使用Stream对象。还必须在服务器和客户端都安装MS XML DOM 3.0 和MDAC 2.5 。
以上就是详细介绍XML技术上传文件的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

How to upload files to 123 Cloud Disk? You can upload files to 123 Cloud Disk for storage, but most friends don’t know how to upload files to 123 Cloud Disk. Next is the picture and text of how to upload files to 123 Cloud Disk brought by the editor for players. Tutorial, interested users come and take a look! How to upload files on 123 Cloud Disk 1. First open 123 Cloud Disk and enter the main page, register or log in to the account; 2. Then enter the page as shown below, click the [Upload] button guided by the arrow; 3. Then the bottom will expand In the function bar window, click the [Select File] function; 4. Finally, select the file to be uploaded and wait patiently for the upload to complete.

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some
