Use XMLHTTP to package HTML data into multipart/form-data format for asynchronous file upload.

Source: Internet
Author: User

Before reading this article, you can read the rfc1867 Form Based File Upload in HTML protocol.

1.
I don't know why, on the LAN, some computers contain <form method = "Post" enctype = "multipart/form-Data"> of <input type = "File>.
The server has used a variety of file receiving tool libraries such as Apache common file upload and jspsmartupload.
I have also changed browsers such as IE and Firefox. the problem persists.

In the search process, some people say that this is a browser restriction, a network problem, or a server network setting, but... no one has pointed out the specific solution,> <!.
However, in many experiments, we finally found that if we use an applet to send the form or use XMLHTTP to package the form into multipart/form-data format, this problem can be solved...

Of course it was not so smooth. First, I used the applet, but I found that the damn JRE security settings do not allow access to local files, which consumes a lot of effort. The applet was successful later, but it was difficult to set up and the JRE should be downloaded.

Then I want to package the data in the browser and send it using a stream. First, I found the Mozilla/Firefox solution on a French website, which strengthened my confidence. Try to use scripting in IE. fileSystemObject and ADODB. stream, but ADODB. stream's poor method of use trapped me. Later, I found the correct method of using the API in the VB forum, and then I converted it into JavaScript.

Then I want to test the data packaging and sending in Firefox. The security settings are no less difficult than JRE. I surrender ~~.

2.
Post related posts now
This post was first found in the Javascript forum, a few years ago. Later, it was found that he encoded the binary into a string and reversed the encoding on the server side, which was a waste of time. In addition, there seemed to be no ready-made anti-encoding class libraries in Java.
Http://community.csdn.net/Expert/topicview.asp? Id = 1546672

In the post on the French website mentioned above, he uses Mozilla and is useless to IE.
Http://xulfr.org/wiki/ApplisWeb/Request

The post of the VB Forum mentioned above.
Http://community.csdn.net/Expert/topic/4407/4407031.xml? Temp =. 8799097.

Thank you for sharing these technologies.

3.
Describes the usage features of ADODB. Stream.
To use ADODB. stream, client ie needs to reduce browser permissions, it is recommended to add the server URL to "trusted site", and then customize the security level, set "initialize ActiveX controls that are not marked as secure and run scripts" to "enable ".

Stream = new activexobject ('ADODB. stream ');
Stream. type = 2; // 2 indicates that the stream type is text.
Stream. charset = "UTF-8 ";
Stream. open ();
Stream. writetext ("test string ");
// After writing the text, you need to write binary content,
// Here is a key point. You need to change the stream type to 1,
// To change the status, set the stream position to 0.
// See how the following works.
Position = stream. position; // record the current position first
Stream. Position = 0; // The position is set to 0.
Stream. type = 1; // Changes the stream status to binary
Stream. Position = position; // restore to the position before change
Stream. Write (binarydata );
// Set the stream position to 0 before sending,
// Because the Microsoft. XMLHTTP object does not help us change the position of ADODB. Stream to 0,
// Read data starting from 0, but starting from the last position of the stream.
Stream. Position = 0; // The position is set to 0.
Httprequest = new activexobject ('Microsoft. xmlhttp ');
Httprequest. Open ("Post", "test. jsp", true );
Httprequest. setRequestHeader ('content-type', 'multipart/form-data, boundary = '+ boundary );
Httprequest. setRequestHeader ("Content-Length", stream. size );
Httprequest. Send (Stream); // send a stream

4.
The complete encapsulation code will not be sent out, which makes no sense. Simply use Cut & copy in the above three posts.

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.