C#上传函数

 

private string SavePhoto(bool isEdit,string oldimgpath)
   {
    string filePath="";
    string allowedExt = "|.jpg|.gif|.jpeg|.png|.bmp|";
    int maxFileSize = 409600;//400KB

    HttpPostedFile postedFile = System.Web.HttpContext.Current.Request.Files["Img"];
    string fileExt = Path.GetExtension(postedFile.FileName).ToLower();
    //检测文件类型是否合法
    if(!isEdit)
    {
     if(allowedExt.IndexOf("|" + fileExt + "|")==-1)
     {
      Response.Write("<script language='JavaScript' type='text/JavaScript'>alert('文件类型不合法,只能上传jpg gif类型文件!');</script>/n");
      Response.End();
     }

     if(postedFile.ContentLength==0)
     {
      Response.Write("<script language='JavaScript' type='text/JavaScript'>alert('请选择您要上传的文件!');</script>/n");
      Response.End();   
     }

     if(postedFile.ContentLength>maxFileSize)
     {
      Response.Write("<script language='JavaScript' type='text/JavaScript'>alert('上传文件大小超出限制,最大只能上传 400KB 的文件!');</script>/n");
      Response.End();
     }
     Random ro=new Random(System.Guid.NewGuid().GetHashCode());
     string tempPath = @"Upload/" + System.DateTime.Now.ToString("yyyy") + @"/" + System.DateTime.Now.ToString("MMdd") + @"/";
     filePath = tempPath + DateTime.Now.ToString("yyyyMMddHHmmss") + ro.Next(10000) + fileExt;
     Functions.CreateFilePath(ConfigurationSettings.AppSettings["PersonalPath"] + tempPath);

     postedFile.SaveAs(ConfigurationSettings.AppSettings["PersonalPath"] + filePath);
    }
    else
    {
     if(postedFile.ContentLength !=0)
     {
      if(allowedExt.IndexOf("|" + fileExt + "|")==-1)
      {
       Response.Write("<script language='JavaScript' type='text/JavaScript'>alert('文件类型不合法,只能上传jpg gif类型文件!');</script>/n");
       Response.End();
      }
      if(postedFile.ContentLength>maxFileSize)
      {
       Response.Write("<script language='JavaScript' type='text/JavaScript'>alert('上传文件大小超出限制,最大只能上传 400KB 的文件!');</script>/n");
       Response.End();
      }
      //删除原有图片
      if( oldimgpath!=null && oldimgpath !=string.Empty)
      {
       string pImgPath = ConfigurationSettings.AppSettings["PersonalPath"] + oldimgpath;
    
       if(System.IO.File.Exists(pImgPath))
        System.IO.File.Delete(pImgPath);
      }
      Random ro=new Random(System.Guid.NewGuid().GetHashCode());
      string tempPath = @"Upload/" + System.DateTime.Now.ToString("yyyy") + @"/" + System.DateTime.Now.ToString("MMdd") + @"/";
      filePath = tempPath + DateTime.Now.ToString("yyyyMMddHHmmss") + ro.Next(10000) + fileExt;
      Functions.CreateFilePath(ConfigurationSettings.AppSettings["PersonalPath"] + tempPath);

      postedFile.SaveAs(ConfigurationSettings.AppSettings["PersonalPath"] + filePath);
     }
     else
     {
      filePath = oldimgpath;
     }
    }

    return filePath;
   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值