Front-end code:
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "upload. aspx. cs" Inherits = "upload" %>
<! DOCTYPE html>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> </title>
<Style type = "text/css">
Li
{
List-style: none;
Padding-top: 10px;
}
</Style>
<Script type = "text/javascript" src = "js/jquery-1.6.2.min.js"> </script>
<Script type = "text/javascript">
Function ValidImage (id, msg ){
$ (Id). parent (). append ("<span>" + msg + "</span> ");
Return false;
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server" enctype = "multipart/form-data" method = "post">
<Div>
<Ul>
<Li>
<Input type = "file" id = "upload1" name = "upload"/>
</Li>
<Li>
<Input type = "file" id = "upload2" name = "upload"/>
</Li>
<Li>
<Input type = "file" id = "upload3" name = "upload"/>
</Li>
<Li>
<Input type = "file" id = "upload4" name = "upload"/> </li>
<Li>
<Input type = "file" id = "upload5" name = "upload"/>
</Li>
<Li>
<Input type = "submit" id = "btnPostFile" runat = "server" onserverclick = "btnPostFile_ServerClick" value = "Start upload"/>
</Li>
</Ul>
</Div>
</Form>
</Body>
</Html>
The foreground is a few controls and a ValidImage method.
Background code:
Copy codeThe Code is as follows:
Protected void btnPostFile_ServerClick (object sender, EventArgs e)
{
String filePath = Server. MapPath ("/uploadImg ");
Const int size = 5242880;
If (! Directory. Exists (filePath ))
{
Directory. CreateDirectory (filePath );
}
If (Request. Files. Count> 0)
{
For (int I = 0; I <Request. Files. Count; I ++)
{
HttpPostedFile postFile = Request. Files [I];
String uploadFileID = string. Format ("# upload {0}", I + 1); // the ID of the current upload control, because jquery needs to be called and added #
String msg = null; // prompt message
If (postFile. FileName. Trim (). Length <= 0)
{
Continue;
}
If (postFile. ContentLength> size)
{
Msg = "the file is too large ";
Page. ClientScript. RegisterStartupScript (GetType (), "", "ValidImage (" + uploadFileID + "," + msg + ")", true); // send the prompt information to the client
Continue;
}
String savePath = Path. Combine (filePath, postFile. FileName); // Save the image address
If (! File. Exists (savePath ))
{
PostFile. SaveAs (Path. Combine (filePath, postFile. FileName); // save if the file does not exist
}
Else
{
Msg = "file" + postFile. FileName + "already exists ";
Page. ClientScript. RegisterStartupScript (GetType (), "", "ValidImage (" + uploadFileID + "," + msg + ")", true); // send the prompt information to the client
Continue;
}
If (IsImg (savePath) // verify whether the file is an image or the format is correct using the IsImg Method
{
SmallImg (postFile. InputStream, postFile. FileName );
}
Else
{
Msg = "only images of the JGP and PNG types can be uploaded. Check whether the file format is correct ";
Page. ClientScript. RegisterStartupScript (GetType (), "", "ValidImage (" + uploadFileID + "," + msg + ")", true); // send the prompt information to the client
File. Delete (savePath); // Delete an image if it is not an image
}
}
}
}
Copy codeThe Code is as follows:
# Region verify the format of the uploaded file
/// <Summary>
/// Verify whether the uploaded file is an image
/// </Summary>
/// <Param name = "FilePath"> file storage path </param>
/// <Returns> </returns>
Private bool IsImg (string FilePath)
{
Using (FileStream fs = new FileStream (FilePath, FileMode. Open, FileAccess. Read ))
{
Bool result = false;
BinaryReader br = new BinaryReader (fs, System. Text. Encoding. UTF8 );
String strImg = "";
Byte buffer;
Try
{
Buffer = br. ReadByte ();
StrImg = buffer. ToString ();
Buffer = br. ReadByte ();
StrImg + = buffer. ToString ();
}
Catch
{
Fs. Close ();
Br. Close ();
}
If (strImg = "255216" | strImg = "13780") // The 255216 is jpg, 7173 is gif, 6677 is BMP, 13780 is PNG, and 7790 is exe, 8297 is rar
{
Result = true;
}
Return result;
}
}
# Endregion
Copy codeThe Code is as follows:
# Region generate thumbnails for images
/// <Summary>
/// Generate a thumbnail
/// </Summary>
Private void SmallImg (Stream oStream, string FileName)
{
Using (System. Drawing. Image img = System. Drawing. Image. FromStream (oStream ))
{
Int newWidth = 100;
Int newHeight = 80;
Int oldWidth = img. Width;
Int oldHeight = img. Height;
If (oldWidth> oldHeight)
{
NewHeight = (int) Math. Floor (double) oldHeight * (double) newWidth/(double) oldWidth );
}
Else
{
NewWidth = (int) Math. Floor (double) oldWidth * (double) newHeight/(double) oldHeight );
}
Using (Bitmap bmp = new Bitmap (newWidth, newHeight ))
{
Using (Graphics g = Graphics. FromImage (bmp ))
{
G. Clear (Color. Transparent );
G. InterpolationMode = InterpolationMode. High;
G. CompositingQuality = CompositingQuality. HighQuality;
G. SmoothingMode = SmoothingMode. HighQuality;
G. DrawImage (img, new Rectangle (0, 0, newWidth, newHeight), new Rectangle (0, 0, oldWidth, oldHeight), GraphicsUnit. Pixel );
String newFileName = Path. GetFileNameWithoutExtension (FileName) + "_ small" + Path. GetExtension (FileName); // thumbnail name
String filePath = Server. MapPath ("/uploadImg/") + newFileName;
Bmp. Save (filePath );
}
}
}
}
# Endregion
The Code has a lot to be improved. I hope you can give more advice.