Foreground code:
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Upload.aspx.cs" inherits= "Upload"%>
<! DOCTYPE html>
<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>
<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 uploading"/>
</li>
</ul>
</div>
</form>
</body>
The front desk is a few controls and a Validimage method.
Background code:
Copy Code code 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 current upload control ID, because jquery is called to add the #
string msg = null; Hint Information
if (PostFile.FileName.Trim (). Length <= 0)
{
Continue
}
if (Postfile.contentlength > Size)
{
msg = "file too large";
Page.ClientScript.RegisterStartupScript (GetType (), "", "validimage (" + Uploadfileid + "," + msg + ")", true); Send a hint to the client
Continue
}
String savepath = Path.Combine (FilePath, postfile.filename); Save address for picture
if (! File.exists (Savepath))
{
Postfile.saveas (Path.Combine (FilePath, postfile.filename)); Save if file does not exist
}
Else
{
msg = "File" + Postfile.filename + "already exists";
Page.ClientScript.RegisterStartupScript (GetType (), "", "validimage (" + Uploadfileid + "," + msg + ")", true); Send a hint to the client
Continue
}
if (isimg (Savepath))//Verify that the file is a picture or is in the correct format by isimg method
{
Smallimg (Postfile.inputstream, postfile.filename);
}
Else
{
msg = "Only JGP, PNG-type pictures can be uploaded, please check that the file format is correct";
Page.ClientScript.RegisterStartupScript (GetType (), "", "validimage (" + Uploadfileid + "," + msg + ")", true); Send a hint to the client
File.delete (Savepath); If it's not a picture, delete it.
}
}
}
}
Copy Code code as follows:
#region Verify the format of the uploaded file
<summary>
Verify that the uploaded file is a picture
</summary>
<param name= "FilePath" > File save 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")//Description 255216 is jpg;7173 is gif;6677 is bmp,13780 is png;7790 is exe,8297 is rar
{
result = true;
}
return result;
}
}
#endregion
Copy Code code as follows:
#region to generate thumbnails of pictures
<summary>
Generate thumbnails
</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
Code has a lot to improve the place, I hope that everyone a lot of guidance.