我有一个表单,用户将他们的图片上传到注册表单..一切正常,直到我添加了一个更新面板和一个图像控件。
我放置了一个更新面板,在其中我放置了一个带有Fileupload控件和图像控件的表。单击按钮后,图像将上载到images文件夹,预览将显示在图像控件中。
在进行修改之前它工作得很好..但是现在我无法读取FileUpload1.Filename ..下面是代码
Reg.aspx
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table> <tr>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload1" runat="server" onclick="btnUpload1_Click"
Text="Upload" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</td>
</tr>
<tr>
<Td>
<asp:Image ID="Image1" runat="server" Width="147px" /></td></tr>
</table>
Reg.aspx.cs
protected void btnUpload1_Click(object sender, EventArgs e)
{
Label1.Text = FileUpload1.FileName;
try
{
var folder = Server.MapPath("~/UploadImages/");
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
Label1.Text = Label1.Text + " Directory Created <br>";
}
var picUrl = Server.MapPath("~/UploadImages/"+ FileUpload1.FileName.ToString());
if (CheckFileName(picUrl) == true)
{
FileUpload1.SaveAs(picUrl);
Image1.ImageUrl = picUrl.ToString();
Label1.Text = Label1.Text + "Image Uploaded";
}
else
Label1.Text = Label1.Text +"File already exists";
}
catch (IOException ioex)
{
Console.WriteLine(ioex.Message);
}
}
private bool CheckFileName(string picUrl) {
if (File.Exists(picUrl))
return false;
return (true);
}
现在我无法从fileupload1控件中读取文件名..
请帮忙
答案 0 :(得分:0)
在更新面板中添加回发触发器。看看下面的代码示例
<强> ASPX:强>
<asp:UpdatePanel runat="server" ID="updatePanel1">
<ContentTemplate>
<asp:FileUpload runat="server" ID="fileUploader"/>
<asp:Button runat="server" ID="buttonSubmit" OnClick="buttonSubmit_OnClick" Text="Submit"/>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="buttonSubmit"/>
</Triggers>
</asp:UpdatePanel>
<强> ASPX.cs 强>
protected void buttonSubmit_OnClick(object sender, EventArgs e)
{
var fileName = this.fileUploader.FileName;
}