asp.net to extract parameters and files from Post data flow _ practical Tips

Source: Internet
Author: User
Tags save file
The same is the case for the simulated POST request. However, it is sometimes possible to simulate post data formats that are not very standard (possibly, not too fine-grained), and are not available on the server side. Recently encountered such a troublesome thing, the data is a partner through the simulation post.

Helpless, thought of a next worst, that is through the analysis of the input stream to extract the desired data from. For example, I mentioned in the above data (two parameters, a picture file)

Realization: Read the input stream, through the non-file part of the analysis, determine the file in the entire flow position and size, and then reread the input stream to get the file.

Look at the post data above, the parameter part and the separator, and so on, plainly are some strings. Basically is the English numeral symbol and so on, if has the Chinese to send before can do the code, this basically can ensure that does not cause the coding problem to make the computation error (the English character each code is the same). This part of the content ( not the file part, the file is part of the binary format, do not do so) can be obtained.
By accepting the input stream as a string, if there is Chinese in the parameter, notice what encoding the client uses when post:

Copy Code code as follows:

byte[] input = Request.BinaryRead (request.totalbytes);
String Source = Encoding.UTF8.GetString (input);

Then the regular expression is used to match the contents of the Non-file section above, and then the resulting content is converted into byte[] to calculate its length. The value of the parameter can be obtained here.
Copy Code code as follows:

Regex rginput = new Regex ("slightly");
if (rginput. IsMatch (source))
{
int Headlength=encoding.utf8.getbytes (rginput. Match (source). Value)
}

Similarly, the length of the "-----------------------------7da119c1004a6--" is calculated at the end (this should be fixed each time). Note that there is a newline return character.

In this way, the size of the part outside the file can be determined in the entire stream, which means that the location and size of the file in the entire stream is determined. Then the file part can be obtained by re-reading the original stream. If the file is of type text, it can be done without this, directly as part of the argument.
Copy Code code as follows:

Save File
FileStream FSS = new FileStream ("path", FileMode.Create);
Fss. Write (input, headlength, input. Length-headlength-footlength);
Fss. Close ();


Code
Copy Code code as follows:

Get file byte array
byte[] Imgcont = new Byte[input. Length-headlength-footlength];
MemoryStream ms = new MemoryStream (input);
The cursor moves to the beginning of the file
Ms. Seek (Headlength,seekorigin.begin);
Ms. Read (Imgcont, 0, Imgcont. Length);
Ms. Close ();

Note: This method has a certain risk, that is, the size and location of the file to determine, must be calculated accurately, to the inflow of analysis to be thoughtful and comprehensive; and when uploading files for multiple, this method is troublesome ... If you have a better way, leave a message to communicate.

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.