<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>
<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;
}