图片上传类(C#)

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// UP 的摘要说明
/// </summary>
public class UP
{
      string fname;
      public UP()
      {
          //
          // TODO: 在此处添加构造函数逻辑
          //
      }

      public string fileup(FileUpload fu, string type, int size, string path, int nametype, bool tt)
      {
          //type为允许上传类型(.jpg|.gif|.bmp),size为上传文件最大为K,path为保存路径(需使用server.mappath),nametype命名方式(1为自动命名),tt为是否生成缩略图
          string erorr = null;
          int Size = fu.PostedFile.ContentLength / 1024;
          if (Size > size)
          {
              erorr = "上传文件太大!";
              return erorr;
          }
          string Type = fu.FileName;
          if (Type.IndexOf(".") == -1) { erorr = "上传文件类型有误1!"; return erorr; }
          Type = Type.Substring(Type.LastIndexOf(".")).ToUpper();
          type = type.ToUpper();
          if (type.IndexOf(Type) == -1) { erorr = "上传文件类型有误!"; return erorr; }

          string filename = "";
          if (nametype == 1)
          {
              string nowdate = DateTime.Now.ToString();
              nowdate = nowdate.Replace(":", "").Replace(" ", "").Replace("-", "").Trim();
              Random r = new Random();
              int a = r.Next(1000);
              filename = nowdate + a.ToString() + Type;
          }
          else
          {
              filename = fu.FileName;
              if (System.IO.File.Exists(path + filename)) { erorr = "此文件名已经存在!"; return erorr; }
          }

          fu.SaveAs(path + filename);


          //生成缩略开始
          if (tt)
          {
              System.Drawing.Image image, aNewImage;
              int width, height, newwidth, newheight;
              System.Drawing.Image.GetThumbnailImageAbort callb = null;
              image = System.Drawing.Image.FromFile(path + filename);
              width = image.Width;
              height = image.Height;
              if (width > height)
              {
                  newwidth = 100;
                  newheight = 100 * height / width;
              }
              else
              {
                  newheight = 100;
                  newwidth = 100 * width / height;
              }
              aNewImage = image.GetThumbnailImage(newwidth, newheight, callb, new System.IntPtr());
              aNewImage.Save(path + "S_" + filename);
              image.Dispose();
          }
          //生成缩略结束
          fname = filename;
          return erorr;//返回有错的错误信息,没有错误返回null
      }

      public string F_name {
          get { return fname; }
      }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值