//图片上传功能:
1.在设计模式下,加文件上传控件。
//第一个Image控件,如果图片上传后,在该控件中显示,Visible控件的初始值为False,其值在.cs页的Button1_Click函数中被改变。第二个是上传控件,需要在.cs页为Button1按钮控件写个OnClick事件函数。第三个Label控件,显示上传成功与否的显示信息。第四个TextBox控件放的是上传路径,该控件和数据库中存放图片路径的字段绑定。
<tr>
<td align="right" style="width: 73px; height: 55px;"><span style="font-size:
<td colspan="3" bgcolor="#ffffff" style="width: 478px; height: 55px;">
<asp:Image ID="Image1" runat="server" Height="150px" Visible="False" Width="120px" /> <br />
<asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" />
<asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ExpertImage") %>'></asp:TextBox>
</td>
</tr>
2.在.cs代码页中,需要写如下函数。
protected void Button1_Click(object sender, EventArgs e)
{
if (((FileUpload)FormView1.FindControl("FileUpload1")).HasFile)
try
{
string strFileName;
DateTime mydatetime = new DateTime();
mydatetime = DateTime.Now;
strFileName = mydatetime.Year.ToString() + mydatetime.Month.ToString() + mydatetime.Day.ToString() + mydatetime.Hour.ToString() + mydatetime.Minute.ToString() + mydatetime.Second.ToString();
string path = HttpContext.Current.Server.MapPath("UploadFile\\");
((FileUpload)FormView1.FindControl("FileUpload1")).SaveAs(path + strFileName);
string strUrl;
strUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
((Image)FormView1.FindControl("Image1")).Visible = true;
((Image)FormView1.FindControl("Image1")).ImageUrl = "UploadFile\\" + strFileName;
((TextBox)FormView1.FindControl("TextBox1")).Text = strUrl + "/jly/manage/UploadFile/" + strFileName;
}
catch (Exception ex)
{
((Label)FormView1.FindControl("Label1")).Visible = true;
((Label)FormView1.FindControl("Label1")).Text = "错误: " + ex.Message.ToString();
}
else
{
((Label)FormView1.FindControl("Label1")).Text = "请选择文件";
}
}