Asp.net simulates the submission of a file upload form (simulate file upload through HTTP)

Source: Internet
Author: User

We will not talk about how to simulate data for the moment. We can use a simple form to see what data the client has submitted to the server when a request occurs.
The following is a simple HTML form, two text input boxes, and one file upload (here I select an image). Note that the enctype attribute of the form to be uploaded is available. CopyCode The Code is as follows: <form action = "SQL. aspx" method = "Post" enctype = "multipart/form-Data">
<Input id = "text1" name = "content" type = "text"/> <br/>
<Input id = "text2" name = "uploadimg" type = "text"/> <br/>
<Input id = "file1" type = "file" name = "image0"/> <br/>
<Input id = "submit1" type = "Submit" value = "Submit"/>
</Form>

To view what data is posted to the server when a form is submitted, I use Fiddler to view the data. Fiddler is indeed a good tool. Note that when the URL host address is localhost, Fiddler cannot be captured. Add a bit (.) After localhost (.) you can open Fiddler, browse the page with the form above, and submit the input data. In Fiddler, you can see the post data. Below is a copy of the data.

It is not difficult to analyze the data. The format of the data field (input type = "text") in a form is

----------------------------- 7da119c1004a6
Content-Disposition: Form-data; name = "content"

This is a TXT Value
The format of a file (input type = "file") is (usually the last parameter of the form)

----------------------------- 7da119c1004a6
Content-Disposition: Form-data; name = "image0"; filename = "E: \ cai \ 875.jpg"
Content-Type: image/pjpeg

[File content]
The end is ----------------------------- 7da119c1004a6 --

With the above data for reference, organize the data according to its format and post it to the server, which can also achieve the HTML form submission effect. Pay special attention to the format: for example, if you press enter to wrap a line, the difference may not be able to get the correct response, and the request Content-Length must be calculated. The following is a reference:

CodeCopy codeThe Code is as follows: Public String postfile (string V1, string V2, string file)
{
String boundary = "---------------------------" + datetime. Now. ticks. tostring ("X ");

// Request
Webrequest Req = webrequest. Create (@ "http: // localhost.: 4944/website1/GetFile. aspx ");
Req. method = "Post ";
Req. contenttype = "multipart/form-data; boundary =" + boundary;

// Organize form data
Stringbuilder sb = new stringbuilder ();
SB. append ("--" + boundary );
SB. append ("\ r \ n ");
SB. append ("content-Disposition: Form-data; name = \" content \"");
SB. append ("\ r \ n ");
SB. append (V1 );
SB. append ("\ r \ n ");

SB. append ("--" + boundary );
SB. append ("\ r \ n ");
SB. append ("content-Disposition: Form-data; name = \" uploadimg \"");
SB. append ("\ r \ n ");
SB. append ("V2 ");
SB. append ("\ r \ n ");

SB. append ("--" + boundary );
SB. append ("\ r \ n ");
SB. append ("content-Disposition: Form-data; name = \" image0 \ "; filename = \" E: \ a.jpg \"");
SB. append ("\ r \ n ");
SB. append ("Content-Type: image/pjpeg ");
SB. append ("\ r \ n ");

String head = sb. tostring ();
Byte [] form_data = encoding. utf8.getbytes (head );
// End
Byte [] foot_data = encoding. utf8.getbytes ("\ r \ n --" + boundary + "-- \ r \ n ");

// File
Filestream = new filestream (file, filemode. Open, fileaccess. Read );
// Total post length
Long length = form_data.length + filestream. Length + foot_data.length;
Req. contentlength = length;

Stream requeststream = Req. getrequeststream ();
// Send form parameters
Requeststream. Write (form_data, 0, form_data.length );
// File Content
Byte [] buffer = new byte [checked (uint) math. Min (4096, (INT) filestream. Length)];
Int bytesread = 0;
While (bytesread = filestream. Read (buffer, 0, buffer. Length ))! = 0)
Requeststream. Write (buffer, 0, bytesread );
// End
Requeststream. Write (foot_data, 0, foot_data.length );
Requeststream. Close ();

// Response
Webresponse Pos = Req. getresponse ();
Streamreader sr = new streamreader (Pos. getresponsestream (), encoding. utf8 );
String html = Sr. readtoend (). Trim ();
Sr. Close ();
If (Pos! = NULL)
{
POs. Close ();
Pos = NULL;
}
If (req! = NULL)
{
Req = NULL;
}
Return HTML;
}

Related Article

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.