asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search








Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Using XML to Improve File-Upload Processing
By Marco Nanni
Rating: 4.1 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Summary

    This article examines an example of multiple binary file upload for Web applications using Extensible Markup Language (XML) technology, without the typical limitation of traditional file upload processing. It describes how to use Microsoft XML Parser 3.0 (MSXML) and ActiveX Data Objects (ADO) Stream objects for a new upload strategy with several benefits. For example, no custom ASP components are required.

    Introduction

    To obtain a file upload with a traditional HTML page, on the client side we can use a form structured in the following way:

    
    <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 solution presents several limitations both on the client and server side. We must use the POST method (because the GET can't manage this type of data), and we have no solutions to trigger a POST processing without using an HTML form. When we send data to the TargetURL, the browser loads this page as the new current page and we have an undesirable "context switch."

    The ENCTYPE property defines the Multipurpose Internet Mail Extensions (MIME) encoding for the form and must be set to "multipart/form-data" for file upload forms. When we set this property to "multipart/form-data" we obtain a different structure of the POST buffer (which is also more complex) and the Request ASP object can't access the form contents. Therefore, we can read the POST buffer using the Request.binaryRead method, but we can't use scripting languages to do this. The Request.binaryRead method returns a VTarray (which is a variant array of unsigned one byte characters) while scripting languages can manage only variant variables. We can resolve this problem only by using a specific, custom ASP component or ISAPI extension, such as CPSHOST.DLL. This behavior is by design.

    A New Upload Strategy

    The idea underlying this article is the use of the following step: On the client-side:

    • create a XML document using the MSXML 3.0 object;
    • create a XML node with binary content;
    • populate this node with the content of the uploading file using the ADO Stream object;
    • send the document to the Web server using the XMLHTTP object.
    On the server-side:
    • read the XML document from the Request ASP object;
    • read the content of the binary node and store it into a file on the server. Optionally, we can store it into a BLOB field of a database tables.
    Before explaining the source code sample, we can make a few considerations about the solution used in this article.

    XML Consideration

    XML support many data types, such as numeric, float, character, etc. Many authors define XML as the ASCII of the future, but we can't forget that this technology can also describe binary information using the "bin.base64" data type. This feature is fully available with MS XML 3.0 Parser and to date requires a custom setup. This object provides some properties that enable a complete management of binary contents:

    obj_node.dataType - This read/write property specifies the data type of the selected node. The XML parser supports more dataType values (see the MSDN reference for a complete list -- http://msdn.microsoft.com/library/psdk/xmlsdk/xmls3z1v.htm).

    For binary contents we can use the "bin.base64" data type;

  • obj_node.nodeTypedValue - This read/write property contains the selected node's value expressed in its defined data type.

    We can create an XML document with more "bin.base64"-type nodes that contain the files we want to upload. This consideration allows the processing of multiple uploading files with a single POST.

    We can use the XMLHttpRequest object to send an XML document to a Web server using the POST method. This object provides client-side protocol support for communication with HTTP servers and allows us to send and receive MS XML Document Object Model (DOM) objects from a Web server. XMLHttpRequest is a built-in COM object with Internet Explorer 5 (not requiring a custom setup) and does not generate a context switch after posting data.

    The ADO Stream Object

    The previous considerations allow the creation (on the client-side) of an XML document with one or more binary nodes. Now we need to populate this node with the contents of the uploading files. Unfortunately, scripting languages can't access the local file system, and the Scripting.FileSystemObject (which is a built-in COM object of the recent Win32 platform) to date can't manage binary files. This behavior is by design. We need an other COM object that provides the access to the local binary files.

    The ADO Stream object (which is a COM object included in the MDAC 2.5 components) provides the means to read, write, and manage a stream of bytes. This byte stream may be text or binary and hasn't particular size limitations. In ADO 2.5, Microsoft has introduced the Stream object without any dependency in the ADO object model hierarchy; therefore, we can use the Stream object without binding it to the other ADO objects.

    In this article, we use the Stream object to access file content and store it into XML node, and vice versa.

    Client-Side Code

    The following code sample provides a client-side file upload using Stream and MSXML objects:

    
    <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</DIV>
    </BODY>
    </HTML>
    
    <SCRIPT LANGUAGE=JavaScript>
    
    // files upload function
    function btn_send.onclick()
    {
       // create ADO-stream Object
       var ado_stream = new ActiveXObject("ADODB.Stream");
    
       // create XML document with default header and primary node
       var xml_dom = new ActiveXObject("MSXML2.DOMDocument");
       xml_dom.loadXML('<?xml version="1.0" ?> <root/>');
       // specify namespaces datatypes
       xml_dom.documentElement.setAttribute("xmlns:dt", "urn:schemas-microsoft-com:datatypes");
    
       // create a new node and set binary content
       var l_node1 = xml_dom.createElement("file1");
       l_node1.dataType = "bin.base64";
       // open stream object and read source file
       ado_stream.Type = 1;  // 1=adTypeBinary 
       ado_stream.Open(); 
       ado_stream.LoadFromFile("c:\\tmp\\myfile.doc");
       // store file content into XML node
       l_node1.nodeTypedValue = ado_stream.Read(-1); // -1=adReadAll
       ado_stream.Close();
       xml_dom.documentElement.appendChild(l_node1);
    
       // we can create more XML nodes for multiple file upload
    
       // send XML documento to Web server
       var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       xmlhttp.open("POST","./file_recieve.asp",false);
       xmlhttp.send(xml_dom);
       // show server message in message-area
       div_message.innerHTML = xmlhttp.ResponseText;
    }
    </SCRIPT>
    
    

    Server-Side Code

    The following code sample provides a server-side file upload using the same objects:

    
    <%@ LANGUAGE=VBScript%>
    <% Option Explicit
       Response.Expires = 0 
       
       ' define variables and COM objects
       dim ado_stream
       dim xml_dom
       dim xml_file1
    
       ' create Stream Object
       set ado_stream = Server.CreateObject("ADODB.Stream")
       ' create XMLDOM object and load it from request ASP object
       set xml_dom = Server.CreateObject("MSXML2.DOMDocument")
       xml_dom.load(request)
       ' retrieve XML node with binary content
       set xml_file1 = xml_dom.selectSingleNode("root/file1")
    
       ' open stream object and store XML node content into it   
       ado_stream.Type = 1  ' 1=adTypeBinary 
       ado_stream.open 
       ado_stream.Write xml_file1.nodeTypedValue
       ' save uploaded file
       ado_stream.SaveToFile "c:\tmp\upload1.doc",2  ' 2=adSaveCreateOverWrite 
       ado_stream.close
    
       ' destroy COM object   
       set ado_stream = Nothing 
       set xml_dom = Nothing
       ' write message to browser
       Response.Write "Upload successful!"
    %>
    
    
    We can also use the ADO Stream to store the uploading file into a BLOB field of a database table. (See the related link for more information.)

    Benefits

    This strategy allows a file upload processing with several benefits:

    • It does not trigger a context switch on the client.
    • No custom ASP components are required.
    • We can use this strategy for multiple binary file upload in a single POST process.
    • This process is totally implemented into a SCRIPT code. We can easily insert this code into a script library since no HTML objects are required. We can also implement this algorithm (on the client-side) using any other language that supports the COM interface, such as Visual Basic (VB), Delphi, PowerBuilder, etc.

    Security and System Consideration

    We can use this solution only for an intranet application because it requires an IE5 security setting with low protection. We must:

    • enable script and activeX controls. This parameter allows the execution of the "myobj = new activexobject(...)" JScript statement;
    • enable access to data source through domain. This parameter allows the use of the Stream object on the client side. We must also install MS XML DOM 3.0 and MDAC 2.5 both on the client and server side.

    References

    About the Author

    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.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Supporting Products/Tools
    AspUpload
    AspUpload is an Active Server component which enables an ASP application to accept, save and manipulate files uploaded with a browser. The files are uploaded via an HTML POST form using RFC 1867. AspUpload can then manipulate the uploaded files in a number of ways which include ACL manipulation, attribute changes, saving to a database, and ActiveX DLL registration.
    [Top]
    ABCUpload
    Uploading files is as simple as ABC with ABCUpload. Our Pure HTML Progress Bar allows your visitors to see the progress of their upload in real time with absolutely no client side software. We also offer a number of other advanced technical features including Unicode Compliant, 120% MacBinary Compatible, BLOB Aware, support for foreign language uploads.

    ABCUpload also supports COM+ and is also available in a .NET version.

    [Top]
    ActiveFile

    With ActiveFile's advanced features, such as restart of interrupted downloads, download failure detection, and industry standard data compression, it's no wonder that companies like Associated Press and Xerox are Infomentum OEM partners. ActiveFile is the professional’s choice for leading edge capabilities that can’t be found in any other file component.

    If you are looking for an intelligent way to exchange files between your ASP or ASP.NET application and web clients, the search is over. Compliant with RFC 1867, ActiveFile provides both file upload and download capabilities that work seamlessly with all of the leading web browsers. Using Active Server Pages or ASP.NET scripting, your application can manipulate files and directories using a robust set of objects and methods provided by the ActiveFile component.

    [Top]
    aspSmartUpload
    aspSmartUpload is a FREE ASP component wich provides you with all the upload/download features you need to transfer files to a server using a browser.
    [Top]
    CyberShop
    An all-in-one shopping cart system that provides a universal hook to all shopping pages on your Web site, regardless what you sell. Runs for all IIS based Web servers under Windows 95, 98, NT 4.0, NT 2000. Works with all versions of FrontPage.
    [Top]
    Other Articles
    Sep 22, 2005 - Implementing Remote Calling Without Using AJAX
    Right now the latest buzzword around town is AJAX. AJAX is an acronym for Asynchronous JavaScript and XML and is a method used to implement remote calling. The problem is that AJAX is only implemented in ASP.NET 2.0. This article will show you one way to implement remote calling without using AJAX or the XMLHttpRequest object. The technique outlined can even be used from classic ASP and is sufficient for most remote calling needs.
    [Read This Article]  [Top]
    Aug 18, 2005 - SQL Server 2005 XQuery and XML-DML - Part 3
    This article is the third and final installment of Alex Homer's series covering the new XML support in Microsoft SQL Server 2005. In it he covers updating the contents of xml columns, comparing traditional XML update techniques with XQuery, and using XQuery in a managed code stored procedure.
    [Read This Article]  [Top]
    Aug 11, 2005 - SQL Server 2005 XQuery and XML-DML - Part 2
    In the second part of his series on SQL Server 2005's new XML support, Alex Homer looks at extracting data from XML columns, comparing traditional XML data access approaches with XQuery, and combining XQuery and XSL-T.
    [Read This Article]  [Top]
    Aug 3, 2005 - SQL Server 2005 XQuery and XML-DML - Part 1
    Microsoft SQL Server 2005 now offers great support for and close integration with XML as a data persistence format. In the first article of his series examining this new support, Alex Homer offers an overview of how SQL Server 2005 stores XML documents and schemas, examines how it supports querying and manipulating XML documents, and provides a simple test application that allows you to experiment with XQuery.
    [Read This Article]  [Top]
    Jun 30, 2005 - Reading and Writing XML in .NET Version 2.0 - Part 3, Cont'd
    In the final article of his series on reading and writing XML in .NET 2.0, Alex Homer looks at how the updated XML document store objects XmlDocument, XmlDataDocument and PathDocument can be used to read, persist and write XML documents and fragments more easily and more efficiently than in .NET 1.x.
    [Read This Article]  [Top]
    Jun 29, 2005 - Reading and Writing XML in .NET Version 2.0 - Part 3
    In the final article of his series on reading and writing XML in .NET 2.0, Alex Homer looks at how the updated XML document store objects XmlDocument, XmlDataDocument and PathDocument can be used to read, persist and write XML documents and fragments more easily and more efficiently than in .NET 1.x.
    [Read This Article]  [Top]
    Jun 16, 2005 - Reading and Writing XML in .NET Version 2.0 - Part 2, Cont'd
    Alex Homer continues his series on reading and writing XML in .NET 2.0. In part one, we focused on the reading side of things, examining the XmlReader and XmlReaderSettings classes. In this article, we move on to look at the XmlWriter and XmlWriterSettings classes, and how they can be used to write XML documents and fragments more easily and more efficiently than in version 1.x of .NET.
    [Read This Article]  [Top]
    Jun 15, 2005 - Reading and Writing XML in .NET Version 2.0 - Part 2
    Alex Homer continues his series on reading and writing XML in .NET 2.0. In part one, we focused on the reading side of things, examining the XmlReader and XmlReaderSettings classes. In this article, we move on to look at the XmlWriter and XmlWriterSettings classes, and how they can be used to write XML documents and fragments more easily and more efficiently than in version 1.x of .NET.
    [Read This Article]  [Top]
    Jun 2, 2005 - Reading and Writing XML in .NET Version 2.0 - Part 1, Cont'd
    In the first part of his series on reading and writing XML in .NET 2.0, Alex Homer discusses the XmlReader and XmlReaderSettings classes. The XmlReader exposes several useful new features and the all new XmlReaderSettings class makes it easy to generate single or multiple instances of an XmlReader with a range of useful properties.
    [Read This Article]  [Top]
    Jun 1, 2005 - Reading and Writing XML in .NET Version 2.0 - Part 1
    In the first part of his series on reading and writing XML in .NET 2.0, Alex Homer discusses the XmlReader and XmlReaderSettings classes. The XmlReader exposes several useful new features and the all new XmlReaderSettings class makes it easy to generate single or multiple instances of an XmlReader with a range of useful properties.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
    Microsoft Article: 7.0, Microsoft's Lucky Version?
    Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Windows Server 2008
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES