C # how to upload images and files to the server using WinForm Uploader. ashx,

Source: Internet
Author: User
Tags define function random seed in domain

C # how to upload images and files to the server using WinForm Uploader. ashx,

There are many solutions on the Internet. At first, I thought FTP was a good implementation because I didn't know much about asp.net, but later I found that if the machine was under domain control, there would be problems.

A year later, asp.net was familiar with it. I knew that ashx should be used for ajax and the verification code should also be used for ashx. Of course, the WinForm upload here should also be ashx. Haha, I would like to provide a simple idea:

The asp.net of the received file is: Uploader. ashx. Related code:

View plaincopy to clipboardprint?
  1. <% @ WebHandler Language = "C #" Class = "Uploader" %>
  2. Using System;
  3. Using System. IO;
  4. Using System. Web;
  5. Public class Uploader: IHttpHandler
  6. {
  7. Public void ProcessRequest (HttpContext hc)
  8. {
  9. Foreach (string fileKey in hc. Request. Files)
  10. {
  11. HttpPostedFile file = hc. Request. Files [fileKey];
  12. File. SaveAs (Path. Combine (hc. Server. MapPath ("."), file. FileName ));
  13. }
  14. }
  15. Public bool IsReusable
  16. {
  17. Get {return true ;}
  18. }
  19. }

WinForm. cs code for sending images or files:

View plaincopy to clipboardprint?
  1. System. Net. WebClient myWebClient = new System. Net. WebClient ();
  2. MyWebClient. UploadFile ("http://www.yongfa365.com/Uploader.ashx", "POST", "C :\\ WINDOWS \ system32 \ cmd.exe ");

OK. After this operation, you no longer have to worry about whether you are in domain control. You can upload data as long as you can access the Internet. It is convenient.

If you want to upload files in batches and the directory in which the files are saved after the upload, refer to the following operations written by the yongfa365 Blog:

The asp.net of the received file is: Uploader. ashx. Related code:

View plaincopy to clipboardprint?
  1. <% @ WebHandler Language = "C #" Class = "Uploader" %>
  2. Using System;
  3. Using System. IO;
  4. Using System. Web;
  5. Public class Uploader: IHttpHandler
  6. {
  7. Public void ProcessRequest (HttpContext hc)
  8. {
  9. String NowPath = Path. Combine (hc. Server. MapPath ("."), hc. Request ["path"]);
  10. If (! Directory. Exists (NowPath ))
  11. {
  12. Directory. CreateDirectory (NowPath );
  13. }
  14. Foreach (string fileKey in hc. Request. Files)
  15. {
  16. HttpPostedFile file = hc. Request. Files [fileKey];
  17. String FilePath = Path. Combine (NowPath, file. FileName );
  18. If (File. Exists (FilePath ))
  19. {
  20. If (Convert. ToBoolean (hc. Request ["overwrite"])
  21. {
  22. File. Delete (FilePath );
  23. }
  24. Else
  25. {
  26. Continue;
  27. }
  28. }
  29. File. SaveAs (FilePath );
  30. }
  31. }
  32. Public bool IsReusable
  33. {
  34. Get {return true ;}
  35. }
  36. }

WinForm. cs code for sending images or files:

View plaincopy to clipboardprint?
  1. String url = @ "http://www.yongfa365.com/Uploader.ashx? Overwrite = true & PATH = Logs \ "+ DateTime. Now. ToString (" yyyy-MM-dd ");
  2. Foreach (string file in Directory. GetFiles (item ))
  3. {
  4. System. Net. WebClient myWebClient = new System. Net. WebClient ();
  5. MyWebClient. UploadFile (url, "POST", file );
  6. }

C language rand () function

Rand and srand usage
First, we need to have a general opinion on rand & srand: srand initializes random seeds, and rand generates random numbers. The following describes in detail.

Rand (Random Number Generation)
Header file: # include <stdlib. h>

Define function: int rand (void)

Function Description:
Because the internal implementation of rand is made by the linear same remainder method, it is not a real random number, but because its cycle is very long, so it can be considered as random within a certain range, rand () returns a random value ranging from 0 to RAND_MAX. Before calling this function to generate a random number, you must use srand () to set the random number seed. If no random number seed is set, rand () will automatically set the random number seed to 1. Rand () generates false random numbers, which are the same during each execution. To be different, initialize it with different values. The initialized function is srand ().

Return Value:
Returns a random integer between 0 and RAND_MAX. The range of RAND_MAX is at least 32767 (int), that is, double byte (16 digits ). If unsigned int is used, the dual-byte value is 65535, and the four-byte value is an integer range of 4294967295.
0 ~ RAND_MAX the probability of each number being selected is the same.

Example:
/* Generate a random value ranging from 1 to 10. In this example, no random seed is set. For the complete random number generation, see
Srand ()*/
# Include <stdlib. h>
Main ()
{
Int I, j;
For (I = 0; I <10; I ++)
{
J = 1 + (int) (10.0 * rand ()/(RAND_MAX + 1.0 ));
Printf ("% d", j );
}
}
Run:

9 4 8 8 10 2 4 8 3 6
9 4 8 8 10 2 4 8 3 6 // re-execution still produces the same random number

Srand (set Random Seed)
Header file: # include <stdlib. h>

Define the function: void srand (unsigned int seed );

Function Description:
Srand () is used to set the random number seed when rand () generates a random number. The seed parameter must be an integer. Generally, the return value of geypid () or time (0) can be used as seed. If the same value is set for each seed, the random values generated by rand () are the same each time.

Example
/* Generate a random number ranging from 1 to 10. This example and execution result can be referenced with rand */
# Include <time. h>
# Include <stdlib. h>
Main ()
{
Int I, j;
Srand (int) time (0 ));
For (I = 0; I <10; I ++)
{
J = 1 + (int) (10.0 * rand ()/(RAND_MAX + 1.0 ));
Printf ("% d", j );
}
}
Execution: Compare with the rand example
5 8 8 8 10 2 10 8 9 9
2 9 7 4 10 3 2 10 8 7
Or:
Use "int x = rand () % 100;" to generate a random number between 0 and 100. This method is not or can be used. A better method is j = (int) (n * rand ()/(RAND_MAX + 1.0) generates a random number between 0 and n.
Int main (void)
{
Int I;
Time_t t;
Srand (unsigned) time (& t ));
Printf ("Ten r... the remaining full text>

C language rand () function

Rand and srand usage
First, we need to have a general opinion on rand & srand: srand initializes random seeds, and rand generates random numbers. The following describes in detail.

Rand (Random Number Generation)
Header file: # include <stdlib. h>

Define function: int rand (void)

Function Description:
Because the internal implementation of rand is made by the linear same remainder method, it is not a real random number, but because its cycle is very long, so it can be considered as random within a certain range, rand () returns a random value ranging from 0 to RAND_MAX. Before calling this function to generate a random number, you must use srand () to set the random number seed. If no random number seed is set, rand () will automatically set the random number seed to 1. Rand () generates false random numbers, which are the same during each execution. To be different, initialize it with different values. The initialized function is srand ().

Return Value:
Returns a random integer between 0 and RAND_MAX. The range of RAND_MAX is at least 32767 (int), that is, double byte (16 digits ). If unsigned int is used, the dual-byte value is 65535, and the four-byte value is an integer range of 4294967295.
0 ~ RAND_MAX the probability of each number being selected is the same.

Example:
/* Generate a random value ranging from 1 to 10. In this example, no random seed is set. For the complete random number generation, see
Srand ()*/
# Include <stdlib. h>
Main ()
{
Int I, j;
For (I = 0; I <10; I ++)
{
J = 1 + (int) (10.0 * rand ()/(RAND_MAX + 1.0 ));
Printf ("% d", j );
}
}
Run:

9 4 8 8 10 2 4 8 3 6
9 4 8 8 10 2 4 8 3 6 // re-execution still produces the same random number

Srand (set Random Seed)
Header file: # include <stdlib. h>

Define the function: void srand (unsigned int seed );

Function Description:
Srand () is used to set the random number seed when rand () generates a random number. The seed parameter must be an integer. Generally, the return value of geypid () or time (0) can be used as seed. If the same value is set for each seed, the random values generated by rand () are the same each time.

Example
/* Generate a random number ranging from 1 to 10. This example and execution result can be referenced with rand */
# Include <time. h>
# Include <stdlib. h>
Main ()
{
Int I, j;
Srand (int) time (0 ));
For (I = 0; I <10; I ++)
{
J = 1 + (int) (10.0 * rand ()/(RAND_MAX + 1.0 ));
Printf ("% d", j );
}
}
Execution: Compare with the rand example
5 8 8 8 10 2 10 8 9 9
2 9 7 4 10 3 2 10 8 7
Or:
Use "int x = rand () % 100;" to generate a random number between 0 and 100. This method is not or can be used. A better method is j = (int) (n * rand ()/(RAND_MAX + 1.0) generates a random number between 0 and n.
Int main (void)
{
Int I;
Time_t t;
Srand (unsigned) time (& t ));
Printf ("Ten r... the remaining full text>

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.