C# FileUpload 在更新面板中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/712695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FileUpload is not working within update panel
提问by Vinod
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.
所以我想要做的是,让用户选择要上传的文件。由于我只接受图像,我将测试扩展。我还想将文件大小限制在 2mb 以下,因此我将对其进行测试(尚未在代码中实现)。如果他们选择的文件通过,那么我希望标签显示“文件已接受”,并存储文件上传信息以供以后单击按钮。一旦用户完成填写表单的其余部分,就会发生这种情况。最终,我将在页面上放置一个 UpdateProgress 控件,同时检查该文件是否被允许。我宁愿不为此发回它,所以如果我可以让它工作,那就太好了。顺便说一句,如果我将标签从更新面板中取出,这一切都会正常工作。
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?
当我运行它时会发生什么,它会转到第一个 if 的 else 语句并返回“请选择一个文件”。这意味着 FileUpload1.HasFile 返回 false。我能看到这种情况发生的唯一原因是 UpdatePanel 无法从 FileUpload 控件访问该信息?
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>
回答by Wim Haanstra
My guess would be that the HasFile will only be filled when the post is already done, not before that.
我的猜测是 HasFile 只会在帖子完成后才会被填充,而不是在此之前。
You might want to check if the FileUpload1.FileName is already filled before the post is done, but I kinda doubt that.
您可能想检查 FileUpload1.FileName 在帖子完成之前是否已经填写,但我有点怀疑。
回答by Gidon
Don't forget to change the type of the form, to allow file uploads (enctype or something like that, i'm not in front of Visual Studio so can't be that precise.)
不要忘记更改表单的类型,以允许文件上传(enctype 或类似的东西,我不在 Visual Studio 前面,所以不能那么精确。)
I had the same problem.
我有同样的问题。
回答by Gthompson83
You're answer can be found here
It is basically disallowed by default because of javascript and browser security reasons. But this is a workaround.
由于 javascript 和浏览器安全原因,它在默认情况下基本上是不允许的。但这是一种解决方法。
回答by Jalpesh Vadgama
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
http://knowledgebaseworld.blogspot.com/2009/02/file-upload-not-working-with-update.html
回答by this. __curious_geek
Default asp.net FileUpload control will never work with UpdatePanel. You need special AsyncFileUploadcontrol as defined in AjaxControl Toolkit. This
默认的 asp.net FileUpload 控件永远不会与 UpdatePanel 一起使用。您需要AjaxControl Toolkit 中定义的特殊AsyncFileUpload控件。这个
http://www.asp.net/ajax/ajaxcontroltoolkit/samples/AsyncFileUpload/AsyncFileUpload.aspx
http://www.asp.net/ajax/ajaxcontroltoolkit/samples/AsyncFileUpload/AsyncFileUpload.aspx
alt text http://ruchitsurati.net/files/fileupload.png
替代文字 http://ruchitsurati.net/files/fileupload.png
<ajaxToolkit:AsyncFileUpload OnClientUploadError="uploadError"
OnClientUploadComplete="uploadComplete" runat="server"
ID="AsyncFileUpload1" Width="400px" UploaderStyle="Modern"
UploadingBackColor="#CCFFFF" ThrobberID="myThrobber" />
回答by Mobenn
Add this line in your page_load
将此行添加到您的 page_load
ScriptManager.GetCurrent(this).RegisterPostBackControl(this.Button);
回答by kiran
Make the the button to upload the file as the trigger of the Upload panel Something like this,
将上传文件的按钮作为上传面板的触发器 像这样,
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="pnlUploadImage" runat="server">
<asp:FileUpload ID="fuldImage" runat="server"></asp:FileUpload>
<asp:LinkButton ID="lnkbUpload" runat="server" onclick="lnkbUpload_Click">Add</asp:LinkButton>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="lnkbUpload"/></Triggers>
</asp:UpdatePanel>
回答by pinki
回答by purav
If you use FileUpload control in Update panel you have to set PostbackTrigger for the button you write the code to save the upload file.
如果您在更新面板中使用 FileUpload 控件,则必须为您编写代码以保存上传文件的按钮设置 PostbackTrigger。
Now Following code I have btnSave button for save the file in the upload folder. So I set the postbacktrigger for it.
现在下面的代码我有 btnSave 按钮,用于将文件保存在上传文件夹中。所以我为它设置了 postbacktrigger。
<Triggers>
<asp:PostBackTrigger ControlID="btnSave" />
</Triggers>
Hope this will help you.
希望这会帮助你。
回答by Pramod
<Triggers>
<asp:PostBackTrigger ControlID="YourControlID" />
</Triggers>
Add the trigger for the UpdatePanel
and give the ControlID
. In case you are using TabContainer
, use the ID of the tab container.
添加触发器UpdatePanel
并给出ControlID
. 如果您正在使用TabContainer
,请使用选项卡容器的 ID。