Image upload package category "including image upload and thumbnail upload". NET

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.