// Creation Time:
// Created by: Yulin Lone Wolf
// Description: Instructions for using the FileUpload File Upload control (only part of it)
//You can upload images and txt files. Doc, wps, and audio files and video files. Powerful functions!
// Front-end code piece, set the Image Upload Interface
<Div>
<Asp: Label ID = "lbText" runat = "server" Font-Bold = "true" Font-Size = "20px" Text = "select the photo you want to upload">
</Asp: Label>
<Br/> <asp: Label ID = "lbImg" runat = "server" ForeColor = "Red"> </asp: Label> <br/>
<Asp: Label ID = "hh" runat = "server" Text = "Upload path:"> </asp: Label> <br/>
<Asp: TextBox ID = "txtFileName" runat = "server"> </asp: TextBox>
<Asp: FileUpload ID = "FileUpLoad1" runat = "server"/>
<Asp: Button ID = "btnUpload" runat = "server" Text = "Start upload" OnClick = "btnUpload_Click"/> <br/>
<Asp: Label ID = "hj" runat = "server" Text = "The uploaded image is as follows"> </asp: Label>
<Br/> <asp: Image ID = "img" runat = "server"/>
</Div>
// Background code for uploading images from the client to the server
Protected void btnUpload_Click (object sender, EventArgs e)
{// HasFile gets a value indicating whether the FileUpload control contains a file.
If ()
{
// PostedFile obtains the basic HttpPostedFile object by using the File Uploaded By the FileUpload control. Public HttpPostedFile
// PostedFile {get ;}
String fileContentType = FileUpLoad1.PostedFile. ContentType;
// You can also use file. Extension to determine the file type, as shown in figure
If (fileContentType = "image/jpeg" | fileContentType = "image/bmp" | fileContentType = "image/gif" |
FileContentType = "image/png ")
{
// Client file path
String name = FileUpLoad1.PostedFile. FileName;
// File name
String filename = file. Name;
// Server File Path
This.txt FileName. Text = filename;
String webFilePath = Server. MapPath ("Upload/" + filename );
// Determine whether the same file exists
If (! File. Exists (webFilePath ))
{
Try {
// Use SqveAs () to save the content of the uploaded file to the specified path on the Web Server
This. lbImg. Text = "prompt: the file" "+ filename +" is uploaded successfully! ";
This. img. ImageUrl = "Upload/" + filename;
}
Catch (Exception ex ){
This. lbImg. Text = "File Upload Failed because" + ex. Message;
}
}
}
Else
{This. lbImg. Text = "the file type does not match ";
}
}
}