Using XMLHTTP to send very long XML form data

Source: Internet
Author: User
Tags object end client
xml| data

When you send a lot of XML as part of your post data to your IIS server--such as in the textarea of an ASP form--you may get some unexpected results. When data is processed on the server, you may end up encountering errors due to the way you handle the data. The reason for this is that when you submit the data back to the server, there is a (data) size limit in the post field. The purpose of this is to prevent potential intruders from sending large amounts of data to the server in an attack that implements a denial-of-service (denial of service,dos).

This restriction also binds you to your abilities. But there are ways to solve the problem. If you are not limited to sending data through form submissions, you can use the XMLHTTP object (a DOM object in Microsoft's XML set) to send the required XML:

var oxmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
Oxmlhttp.open ("POST", "xml_handler.asp", false);
Oxmlhttp.send (xml_to_send);

Because the request object implements the IStream interface, you can use the DOMDocument object'sload ()method to load the XML to be submitted:

Dim ODOM
Set ODOM = Server.CreateObject ("MSXML2. DOMDocument ")
Odom.load Request

If you are limited to using form submission, you can cross this limit by submitting multiple textarea or input, both of which can be grouped together once the server receives the form data:

var maxlen = 90000;
var oform = document.createelement ("FORM");
Oform.method = "POST";
Oform.action = "xml_handler.asp";
Oform = Document.body.appendChild (oform);
var s = document.someForm.txtXML.value;
if (S.length > MaxLen) {
while (S.length > MaxLen) {
var o = document.createelement ("INPUT");
O.type = "hidden";
O.name = "Txtxml";
O.value = s.substr (0, MaxLen);
Oform.appendchild (o);
s = s.substr (MaxLen);
}
var o = document.createelement ("INPUT");
O.type = "hidden";
O.name = "Txtxml";
O.value = s.substr (0, MaxLen);
Oform.appendchild (o);
} else {
var o = document.createelement ("INPUT");
O.type = "hidden";
O.name = "Txtxml";
O.value = s;
Oform.appendchild (o);
}

This section of code creates a new form element that handles the submission of the data and places it inside the BODY element. It then checks the length of the XML that is about to be submitted to the server. This XML resides within the Someform, a textarea called txtxml.

If the XML is more than 90,000 characters MaxLen, then the code creates multiple hidden input elements, sets the property of the value to 90,000-character XML data, or sets it to a value at the end of the XML, dividing the data into parts. If the XML size is less than maxlen, then the code creates only one input and sets the value accordingly. The data is then submitted to the server for processing.

You may have noticed that I assign the same name--txtxml--to each field of the new form. This will help isolate the XML data from other data that might be submitted and provide an easy way to reorganize the XML data. When reorganizing data, you need a simple loop to connect the data in the field:

Dim str, FLD
For each fld in Request.Form ("Txtxml")
str = str & FLD
Next

Because a field set has been created for each form element, you can iterate over the field in the same name. You don't have to worry about the order in which the fields are traversed as long as the form element is created in the appropriate order on the client. This can be easily implemented through the appendchild () method of form.

The data is submitted in the order of the client from left to right, from top to bottom, so when you attach the input element to the end of the form element, you always receive the data in the same order on your server.

If you're looking to implement a large data solution, such as sending a large amount of Excel data from a client machine to a server, you should reconsider whether you want to use form submission or logically divide the data into smaller pieces. Since you cannot use the file type INPUT element, the most creative solution is to transform the data locally into XML and then submit the XML data to the server. Conversely, the data is saved on the server until further processing is required.

Of course, there may be a better way to deal with this problem. But when you don't have much time, all you need is a quick, usable solution.



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.