文件上传不更新面板内工作 [英] FileUpload is not working within update panel
问题描述
所以,我所要做的是,有一个用户选择要上传的文件。因为我只打算接受图像,我将测试扩展。我也想将文件大小限制为小于2MB,所以我会测试(没有在code语言实现的还)。如果他们选择的文件传递,那么我想在标签说文件已接受,并存储在文件上传信息,以后按一下按钮。一旦用户完成填写表单的其余部分会出现这种情况。最后,我把一个UpdateProgress控件的页面上,而它如果文件被允许正在检查。我宁可不要这回发了这一点,所以如果我能得到它的工作,那将是巨大的。顺便说一句,如果我走了标签更新面板这都将正常工作。
会发生什么事,当我运行这一点,是它会去的第一个,如果和返回else语句请选择一个文件。这意味着FileUpload1.HasFile是返回false。我可以看到,出现这种情况的唯一原因是因为在UpdatePanel无法从FileUpload控件访问这些信息?
code背后:
标签SubmitButtonLabel2 =(标签)UpdatePanel1.FindControl(SubmitButtonLabel);
如果(FileUpload1.HasFile)
{
字符串[]文件名= FileUpload1.FileName.Split('。');
如果((文件名[fileName.Length - 1] ==JPG)||
(文件名[fileName.Length - 1] ==GIF)||
(文件名[fileName.Length - 1] ==BMP)||
(文件名[fileName.Length - 1] ==JPEG)||
(文件名[fileName.Length - 1] ==PNG))
{
SubmitButtonLabel2.Text =文件接受。
}
其他
{
SubmitButtonLabel2.Text =文件类型不允许的,请选择其他。
}
}
其他
{
SubmitButtonLabel.Text =请选择一个文件。
}
页:
<身体GT;
<表ID =form1的=服务器>
< ASP:的ScriptManager ID =ScriptManager1=服务器/>
< DIV>
< ASP:文件上传ID =FileUpload1=服务器/>
< ASP:按钮的ID =提交按钮=服务器文本=提交文件的OnClick = SubmitButton_Click />
< ASP:的UpdatePanel ID =UpdatePanel1=服务器的UpdateMode =永远>
<&的ContentTemplate GT;
< ASP:标签ID =SubmitButtonLabel=服务器/>
< /&的ContentTemplate GT;
<&触发器GT;
< ASP:PostBackTrigger控件ID =提交按钮/>
< /触发器>
< / ASP:的UpdatePanel> < / DIV>
< /表及GT;
< /身体GT;
没有必要做任何事情,你只需要多部分数据attribut添加到您的表单。
Page.Form.Attributes.Add(ENCTYPE,多部分/表单数据);
详细信息请参见以下链接:
<一个href=\"http://knowledgebaseworld.blogspot.com/2009/02/file-upload-not-working-with-update.html\">http://knowledgebaseworld.blogspot.com/2009/02/file-upload-not-working-with-update.html
So what I am trying to do is, have a user select a file to upload. Since I am only going to accept images, I will test the extension. I also want to limit the file size to under 2mb, so I will test that(haven't implemented in code yet). If the file they have selected passes, then I want the label to say "File Accepted", and store the file upload information for a later button click. This will happen once the user has finished filling out the rest of the form. Eventually, I will put an UpdateProgress control on the page while it is checking if the file is allowed. I would rather not have it post back for this, so if I can get it to work, that would be great. BTW, this will all work fine if I take the label out of the update panel.
What happens when I run this, is it will go to the else statement of the first if and return "Please select a file." Meaning that FileUpload1.HasFile is returning false. The only reason I can see that this is happening is because the UpdatePanel can not access that info from the FileUpload control?
Code Behind:
Label SubmitButtonLabel2= (Label)UpdatePanel1.FindControl("SubmitButtonLabel");
if (FileUpload1.HasFile)
{
string[] fileName = FileUpload1.FileName.Split('.');
if ((fileName[fileName.Length - 1] == "jpg") ||
(fileName[fileName.Length - 1] == "gif") ||
(fileName[fileName.Length - 1] == "bmp") ||
(fileName[fileName.Length - 1] == "jpeg") ||
(fileName[fileName.Length - 1] == "png"))
{
SubmitButtonLabel2.Text = "File Accepted.";
}
else
{
SubmitButtonLabel2.Text = "File type not allowed. Please choose another.";
}
}
else
{
SubmitButtonLabel.Text = "Please select a file.";
}
Page:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="SubmitButton" runat="server" Text="Submit File" OnClick=SubmitButton_Click />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="always">
<ContentTemplate>
<asp:Label ID="SubmitButtonLabel" runat="Server" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="SubmitButton" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
No need to do anything you just need to add multipart data attribut to your form.
Page.Form.Attributes.Add("enctype", "multipart/form-data");
See the following link for more details.
http://knowledgebaseworld.blogspot.com/2009/02/file-upload-not-working-with-update.html
这篇关于文件上传不更新面板内工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!