上传图片的方法以及判断

<asp:FileUpload ID="fulImage" runat="server" Width="346px" /><br />
<asp:Image ID="imgPro" runat="server" Width="118px" Height="88"/><font color="red">(图片大小:300*226px)</font>
<asp:Button ID="btnUpImg" AccessKey="1" runat="server" Text="上传图片" onclick="btnUpImg_Click" Width="96px" Height="30px" />

if (imgPro.ImageUrl.Substring(imgPro.ImageUrl.LastIndexOf("/") + 1).Equals("productDemo.gif"))
strErr.Append("请上传图片!");
string proimg = imgPro.ImageUrl;

protected void btnUpImg_Click(object sender, EventArgs e)
    {
        string picName = "";
        string file = "/images/small/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "-" + DateTime.Now.Day + "/";
        SetFocus("btnSubmit");
        UploadPicture.CreatDirectory(file);
        txtName.Text = fulImage.FileName.Replace(".gif", "").Replace(".jpg", "");
        if (fulImage.FileName != null && fulImage.FileName != "")
        {
            picName = UploadPicture.UploadInfo(fulImage, file);

            if (picName == null)
                Response.Write("<script>alert('图片上传失败!')</script>");
            else
                imgPro.ImageUrl = UploadPicture.formatPics(picName);
        }
    }





public static string UploadInfo(FileUpload fu, string file)
    {
        Random myRd = new Random();

        string prefix = System.IO.Path.GetExtension(fu.FileName).ToLower();
        string fileName = "";
        if (prefix.Equals(".jpg") || prefix.Equals(".gif") || prefix.Equals(".jpeg"))
        {
            string strFileName = DateTime.Now.ToString().Replace("-", "").Replace(":", "").Replace(" ", "") + myRd.Next(1000) + prefix;
            string name = System.Web.HttpContext.Current.Server.MapPath(file + strFileName);
            fileName = file + strFileName;
            fu.SaveAs(name);
            return fileName;
        }
        else
            return null;
    }

  

原文地址:https://www.cnblogs.com/candyzhmm/p/4713552.html