/* Title: single man/woman
* Author: Insun
* Blog: http://yxmhero1989.blog.163.com
* From: http://web087429.s2.eiisp.com.cn/forum.php
*/
0x00. First, write a simple image for upload.
Use the FileUpload control to add a Button event to define the permitted form of the uploaded image. The label displays some image information, and use try .. Catch .. Format processing may be abnormal. First, create an image folder images. The saved file name is set to yyyyMMddhhmmssfff in the following example to avoid conflict.
// Configure //-----------------------------------------------------------------------------------------------
// -- The code is really simple. Children can understand it, right?
Protected void button#click (object sender, EventArgs e) // The content is overwritten by the same name and will not be renamed !!! Modify it !!
{
Bool fileIsValid = false;
String strImageName = "";
String Publishtime = DateTime. Now. ToString ();
If (this. FileUpload1.HasFile)
{
String fileExtension = System. IO. Path. GetExtension (this. FileUpload1.FileName). ToLower ();
String [] restrictExtension = {". gif", ". jpg", ". bmp", ". png "};
For (int I = 0; I <restrictExtension. Length; I ++)
{
If (fileExtension = restrictExtension [I])
{
FileIsValid = true;
}
}
If (fileIsValid = true)
{
Try
{
// Solution 1: Use the GUID structure to generate a unique file name
// Solution 2: Use the time to generate a new file name (Year + month + day + hour + minute + second + millisecond)
String SaveName = DateTime. Now. ToString ("yyyyMMddhhmmssfff"); // generate an image name based on time
StrImageName = SaveName + this. FileUpload1.PostedFile. FileName. Substring (this. FileUpload1.PostedFile. FileName. LastIndexOf ("."); // Add the image name with the suffix
// String strpath = Server. MapPath ("") + "\ UpImages \" + strImageName; // obtain the path to save the image
This. Image1.ImageUrl = "~ /Images/"+ strImageName;
This. FileUpload1.SaveAs (Server. MapPath ("~ /Images/") + strImageName); // you need to create an image directory. If not, delete it ~ /Images /"
// This. FileUpload1.SaveAs (Server. MapPath ("~ /Images/") + FileUpload1.FileName); // you need to create an image directory. If not, delete it ~ /Images /"
// The image name is changed to the current time when it is uploaded, so that when there are too many images, the original image will not be overwritten, and the size of the image will be controlled. When you select the correct image, the IMAGE is displayed in the IMAGE control.
This. Label1.Text = "File Uploaded successfully! ";
This. Label1.Text + = "<br/> ";
This. Label1.Text + = "<li>" + "original file path:" + this. FileUpload1.PostedFile. FileName;
This. Label1.Text + = "<br/> ";
This. Label1.Text + = "<li>" + "file size:" + this. FileUpload1.PostedFile. ContentLength + "Byte ";
This. Label1.Text + = "<br/> ";
This. Label1.Text + = "<li>" + "file type:" + this. FileUpload1.PostedFile. ContentType;
This. Label1.Text + = "<br/> ";
This. Label1.Text + = "<li>" + "InputStream:" + this. FileUpload1.PostedFile. InputStream;
This. Label1.Text + = "<br/> ";
This. Label1.Text + = "<li>" + "HashCode:" + this. FileUpload1.PostedFile. GetHashCode ();
This. Label1.Text + = "<br/> ";
This. Label1.Text + = "<li>" + "operation time:" + SaveName;
}
Catch
{
This. Label1.Text = "File Upload Failed! ";
}
}
& Nb