The original Published time: 2009-08-30--from my Baidu article [imported by moving tools]
#region upload images and upload thumbnails
public class UpFile:System.Web.UI.Page
{
<summary>
Upload Image Jpg/jpeg/gif/bmp Format
Created: 2009-06-09
Author: handboy
</summary>
private string fileName = String.Empty;
public string FileName
{
get {return fileName;}
set {fileName = value;}
}
<summary>
Upload the image, you need to set the upload directory
</summary>
<param name= "Postfile" >FileUpload1.PostedFile</param>
<param name= "Postdir" > uploaded directory. such as: "Upload" </param>
///
Invocation example: webpublic.upfile uf = new Webpublic.upfile ();
Uf.upfile (Fileupload1.postedfile, "Upload", 150,120);
///
public void Upfile (Httppostedfile postfile, string postdir, int width, int height)
{
if (PostFile.FileName.Length > 0)
{
int filelength = Postfile.contentlength;
if (filelength/1024 <= 500)
{
string filecontenttype = Postfile.contenttype;
if (Filecontenttype = = "Image/pjpeg" | | filecontenttype = = "Image/gif" | | filecontenttype = "Image/bmp")
{
Picture renaming
String nowstr = DateTime.Now.ToString ("Yyyymmddhhmmss");
Get file name extension
String fileType = PostFile.FileName.Substring (PostFile.FileName.LastIndexOf (".")). ToLower ();
if (fileType! = ". gif")
FileType = ". jpg";
FileName = Nowstr + fileType;
Upload picture path
String dir = Server.MapPath (Postdir);
if (! Directory.Exists (dir))
{
Directory.CreateDirectory (dir);
}
String savepath = dir + "/" + fileName;
if (file.exists (Savepath))
{
Alert ("Upload a picture at this time too many uploads failed, please try again!");
}
Else
{
Postfile.saveas (Savepath);
if (width! = 0 && Height! = 0)
{
String minpath = dir + "/min_" + fileName;
Image img = image.fromfile (savepath);
System.Drawing.Bitmap bmp = New Bitmap (img, width, height);
Bmp. Save (Minpath);
Bmp. Dispose ();
Img. Dispose ();
}
Alert ("Picture uploaded successfully!");
}
}
Else
{
Alert ("The picture is not properly formatted!");
}
}
Else
{
Alert ("The picture you want to upload is too big please upload a picture within 500K!");
}
}
Else
{
Alert ("Please select picture!");
}
}
//<summary>
// /upload image, default directory is upload
//</summary>
///
//Call Example: webpublic.upfile uf = new Webpublic.upfile ();
/// Uf.upfile (Fileupload1.postedfile);
///
public void Upfile (Httppostedfile postfile)
{
Upfile (Postfile, "Upload", 0, 0);
}
///<summary>
/ Upload images and thumbnails, default directory is upload
//</summary>
///
//webpublic.upfile UF = new Webpublic.upfile ();
//Uf.upfile (Fileupload1.postedfile, 150,120);
///
public void Upfile (httppostedfile postfile, int width, int height)
{
upfile (Postfile, "Upload", width, height);
}
#region Tips
public static void alert (String stralert)
{
string temp;
temp = "<script language= ' JavaScript ' >";
Temp + = "alert ('" + Stralert + "');";
Temp + = "</Script>";
System.Web.HttpContext.Current.Response.Write (temp);
}
#endregion
}
#endregion
Image upload package category "including image upload and thumbnail upload". NET