ASP + XML file upload

Source: Internet
Author: User

<Font size = 18> test.htm:
<HTML>
<Head>
<Title> file send </title>
</Head>
<Body>
<Input id = filename name = filename type = File>

 

<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 ()
{
If (filename. value = "")
{
Alert ("the file does not exist, or the file content is empty. ");
Return false;
}
// Create an ado-Stream Object
VaR ado_stream = new activexobject ("ADODB. Stream ");
// Create an XML document containing the default header information and the root node
VaR xml_dom = new activexobject ("msxml2.domdocument ");
Xml_dom.loadxml ('<? XML version = "1.0"?> <Root/> ');
// Specify the Data Type
Xml_dom.documentelement.setattribute ("xmlns: DT", "urn: Schemas-Microsoft-com: datatypes ");
// Create a new node and set it as a binary data node
VaR file_body = xml_dom.createelement ("file_body ");
File_body.datatype = "bin. base64 ";
// Open the stream object and read the source file
Ado_stream.type = 1; // 1 = adtypebinary
Ado_stream.open ();
Ado_stream.loadfromfile (filename. value );
// Save the file content to the XML Node
File_body.nodetypedvalue = ado_stream.read (-1); //-1 = adreadall
Ado_stream.close ();
Xml_dom.documentelement.appendchild (file_body );
//
// Create a new node and set it as string data
VaR file_name = xml_dom.createelement ("file_name ");
File_name.datatype = "string ";
// Store the file name on this node
File_name.nodetypedvalue = filename. value. tostring ();
Xml_dom.documentelement.appendchild (file_name );
//
// Multiple binary nodes can be created to upload multiple files at a time

// Send the XML document to the Web server
VaR XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
XMLHTTP. Open ("Post", "./test. asp", false );
XMLHTTP. Send (xml_dom );
// Display the information returned by the server
Div_message.innerhtml = XMLHTTP. responsetext;
}
</SCRIPT>

<Font size = 18> test. asp:

<% @ Language = VBScript %>
<%
Option explicit
Response. expires = 0
'Define variables and objects.
Dim ado_stream
Dim xml_dom, filename
Dim xml_file1, xml_file2

'Create Stream Object
Set ado_stream = server. Createobject ("ADODB. Stream ")
'Create an xmldom object from the request object
Set xml_dom = server. Createobject ("msxml2.domdocument ")
Xml_dom.load (request)

'Read the file name
Set xml_file2 = xml_dom.selectsinglenode ("root/file_name ")
Filename = parsefilename (CSTR (xml_file2.nodetypedvalue ))

'Read the node that contains binary data
Set xml_file1 = xml_dom.selectsinglenode ("root/file_body ")

'Open the stream object and store the data in it.
Ado_stream.type = 1' 1 = adtypebinary
Ado_stream.open
Ado_stream.write xml_file1.nodetypedvalue
'File storage disks'
Dim filepath
If right (server. mappath ("Upload"), 1) <> "/" then
Filepath = server. mappath ("Upload") + "/" + filename
Else
Filepath = server. mappath ("Upload") + filename
End if
Ado_stream.savetofile filepath, 2' = adsavecreateoverwrite
Ado_stream.close
'Destroy object
Set ado_stream = nothing
Set xml_dom = nothing
'Return information to the browser
Response. Write "Upload successful! "+ Filename

Function parsefilename (sfilein)
Dim I
For I = Len (sfilein) to 1 step-1
If instr ("/", mid (sfilein, I, 1) Then exit
Next
Parsefilename = mid (sfilein, I + 1, Len (sfilein)-I)
End Function
%>

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.