您的位置:首页 > 其它

上传图片的方法以及判断

2015-08-08 17:31 387 查看
<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;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: