上传图片2

<tr>
                             <td align="right">推荐小图:</td>
                             <td align="left" colspan="3">
                                 <asp:FileUpload ID="fulImage" runat="server" Width="407px" /><br />
                                 <asp:Image ID="imgBus" runat="server" Width="140px" Height="100px" ImageUrl="/Images/Demo.gif"/><font color="red">(图片大小:

300*214px)</font>
                                 &nbsp;&nbsp;&nbsp;&nbsp;
                                 <asp:Button ID="btnUpImg"
                                     runat="server" Text="上传图片" onclick="btnUpImg_Click" Width="100px" Height="30px" />
                             </td>
                         </tr>

 //推荐大图片
            string bigpic;
            if (imgbigBus.ImageUrl.Equals("~/Images/businessDemo.gif"))
                bigpic = "";
            else
                bigpic = imgbigBus.ImageUrl.Trim();
  protected void btnUpImg_Click(object sender, EventArgs e)
    {
        string picName = "";
        string file = "/images/CollegeImages/";

        if (fulImage.FileName != null && fulImage.FileName != "")
        {
            picName = UploadPicture.UploadInfo(fulImage, file);

            if (picName == null)
                Response.Write("<script>alert(‘图片上传失败!‘)</script>");
            else
                imgBus.ImageUrl = 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") || prefix.Equals(".png"))
        {
            string strFileName = DateTime.Now.ToString().Replace("-", "").Replace(":", "").Replace(" ", "") + myRd.Next(1000) + prefix;
            string name = @System.Web.HttpContext.Current.Server.MapPath(file.Replace("~", "") + strFileName);
            //System.Web.HttpContext.Current.Server.MapPath(file + strFileName);
            fileName = file + strFileName;
            name = name.Replace("zhuzhan\\images", "images");
            fu.SaveAs(name);
            return fileName;
        }
        else
            return null;
    }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。