asp.net上传图片并判断是否存在限制图片大小

3 篇文章 0 订阅
 
 public bool Upload()
    {
        string username = HttpContext.Current.User.Identity.Name;

        string file = fldPhoto.FileName;    //

        if (file.Length == 0)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择要上传的文件');</script>");
            return false;
        }
        else
        {
            if (fldPhoto.PostedFile.ContentLength >= 2000000)  //2M
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传文件不能超过2M!');</script>");
                return false;
            }
            else
            {
                return true;
            }
        }
       
    }
public void UploadFiles()
    {
        string strCompanyName = "";
        if (Request["uid"] != null)
        {
            strCompanyName = Request["uid"].Substring(0, 5) + "/" + Request["uid"].Substring(0, 9);
        }
        //获取文件名
        string strPhotoName ="";
        FileInfo file = new FileInfo(fldPhoto.PostedFile.FileName);
 //Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + file.Extension + "');</script>");
        //if (fldPhoto.PostedFile.ContentType == "image/gif" || fldPhoto.PostedFile.ContentType == "application/x-jpg" || fldPhoto.PostedFile.ContentType == "image/jpeg")
        //{

 if (file.Extension == ".jpg" || file.Extension == ".gif")
 {
     strPhotoName = StandardID() + file.Extension;
     //获取相对地址
     string strPhotoNamePath = Server.MapPath("File/TypeNotice/");
     strPhotoName = System.IO.Path.GetFileName(strPhotoName);
     //文件的安全地址
     string strFullFileName = strPhotoNamePath + strCompanyName + "\\" + strPhotoName;
     //文件存放文件夹地址
     string strMapPath = strPhotoNamePath + strCompanyName;
     //判断文件夹是否存在
     try
     {
         if (System.IO.Directory.Exists(strMapPath))
         {
             if (System.IO.File.Exists(strFullFileName))
             {
                 ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('已经存在!');</script>");
             }
             else
             {
                 //string inUploadFileName = StandardID();
                 //string inFileDir = this.Server.MapPath(strFullFileName) + file.Extension;
                 //string inFileDir = this.Server.MapPath("UploadFiles/") + inUploadFileName;
                 fldPhoto.SaveAs(strFullFileName);
                 //UploadFile.SaveAs(inFileDir);
                 txtFile.Text = strPhotoName;
                 //Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + fldPhoto.PostedFile.ContentLength.ToString() + "');</script>");
             }
         }
         else
         {
             //创建文件夹
             System.IO.Directory.CreateDirectory(strMapPath);
             fldPhoto.SaveAs(strFullFileName);
             txtFile.Text = strPhotoName;
         }
     }
     catch (Exception e)
     {

         ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加失败!');</script>");
     }
 }
 else 
 {
     Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('文件类型错误!');</script>");
 }
        //}
        //else
        //{
        //    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('文件类型错误!');</script>");
        //}

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值