自动上传文件到服务器的更新面板内不工作的第一次 [英] Uploading a file to server automatically inside an update panel does not work the first time
问题描述
要求
我想上传一个文件,只要用户选择它。我必须满足以下要求:
I am trying to upload a file, as soon as a user selects it. I have to fulfill the following requirements:
- 按钮看起来像应用程序的其他按钮。
- 的文件,只要用户选择其上传
- 我需要它是一个UpdatePanel,因为我必须做出有条件的更新页面。 我CAN 做选择的文件完全回发(a.k.a
的onchange
)的事件。
- The button looks like other buttons in the application.
- The file is uploaded as soon as the user selects it.
- I need it to be in an UpdatePanel as I have to make conditional updates to the page. I CAN do a full postback on the file selected (a.k.a
onchange
) event.
当前code
以下是我查看文件的外观:
Following is how my view file looks:
<asp:UpdatePanel ID="upData" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<div style="width: auto; float: right;">
<asp:Button ID="btnFileImportSkin" CssClass="ButtonSkin AddButton" Text="Import" Style="position: absolute; z-index: 2;" runat="server" OnClientClick="Javascript:onImport(); return false;" />
<asp:FileUpload ID="fileImport" Visible="false" Style="position:relative; opacity:0;" runat="server" onchange="Javascript:onFileSelected();" />
<%-- onchange="Javascript:this.form.submit();" /> --%>
<%-- <asp:Button ID="btnUpload" runat="server" OnClientClick="Javascript:alert('Uploading...'); __doPostBack('<%= btnUpload.ID %>', ''); return false;" /> --%>
<asp:Button ID="btnUpload" OnClick="btnUpload_Click" runat="server" />
</div>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnUpload" />
</Triggers>
</asp:UpdatePanel>
相应的JavaScript:
Relevant Javascript:
<script type="text/javascript">
function onImport() {
var fileImportClientId = '<%= fileImport.ClientID %>';
document.getElementById(fileImportClientId).click();
}
function onFileSelected() {
alert("File Selected");
// I have tried calling the function directly and with a timeout
setTimeout(onUpload, 20);
}
function onUpload() {
var btnUploadClientId = '<%= btnUpload.ClientID %>';
document.getElementById(btnUploadClientId).click();
}
</script>
背后code:
protected void btnUpload_Click(object sender, EventArgs e)
{
// PostedFile is null first time code gets here on user selecting a file
if (fileImport.PostedFile != null)
{
if (fileImport.PostedFile.FileName.Length > 0)
{
ImportFromFile();
}
}
}
解释/流量
- 在
btnFileImportSkin
按钮,用户点击。 - 函数
onImport
被调用,它编程点击了fileimport
按钮。 - 用户选择一个文件,presses打开。
-
onFileSelected
被调用。 -
onUpload
调用成功。 -
btnUpload_Click
成功每次调用。
- User clicks on
btnFileImportSkin
button. - The function
onImport
is called, which programmatically clicks on thefileimport
button. - User selects a file, and presses Open.
onFileSelected
is called.onUpload
is called successfully.btnUpload_Click
is called successfully every time.
然而在问题是
fileImport.PostedFile是空的第一次用户选择文件。一切工作正常,第二次,并从那里。
fileImport.PostedFile is null the first time user selects a file. Everything works fine the second time and from there on.
相关
<一个href=\"http://stackoverflow.com/questions/8310832/upload-a-file-to-server-automatically-inside-a-update-panel-in-asp-net-website\">This问题密切相关,我的问题,但可能OP想要一个异步上传解决方案,在Gmail中。我已经尝试做以下作为这个问题的答案:
This question is closely related to my problem, but the OP probably wanted an Async upload solution as in Gmail. I have already tried doing the following as in the answers to this question:
- __ doPostBack()中的OnClientClick事件
btnUpload
- this.form.submit()我的
文件上传
控制onchange事件。 - 设置
文件上传
控件的onchange属性中Page_ preRender
- __doPostBack() in OnClientClick event of
btnUpload
- this.form.submit() onchange event of my
FileUpload
control. - Setting the onchange attribute of
FileUpload
control in Page_PreRender
其他注意事项
- 这件事情非常完美的时候,我没有更新面板。 )直接在
文件上传
控制onchange事件我在做this.form.submit(。 - 目标框架是.NET 4.0
- This thing worked perfectly when I did not have update panels. I was doing this.form.submit() directly in onchange event of
FileUpload
control. - Target framework is .NET 4.0
请注意:添加一个以上可见=在文件上传
假的控制。这是问题,但我忽略了它,而问的问题。
NOTE: Added a Visible="false" in FileUpload
control above. It was the problem but I had ignored it while asking question.
推荐答案
从 BeeTee的回答,另一个提示< A HREF =http://stackoverflow.com/q/11575716/365188> SO帖子和的在另一个论坛这个线程,我发现原来的文件上传的控制必须是prerendering 中可见。这可能是适用于任何家长的的UpdatePanel 的这种控制在于所以,基本上我删除可见=假
从下面我的控制定义:
With a hint from BeeTee's answer, another SO post and this thread on another forum, I found out that the FileUpload control must be visible during prerendering. This is probably true for any parent UpdatePanels this control lies in. So, basically I removed Visible=false
from my control definition below:
<asp:FileUpload ID="fileImport" Visible="false" Style="position:relative; opacity:0;" runat="server" onchange="Javascript:onFileSelected();" />
在我的情况,我可以通过设置的 CSS 的透明度
属性设置为0。但是,还有它周围的其他两种方式隐藏:
In my case I could hide it by setting the css opacity
property to 0. However, there are two other ways around it:
- 将 CSS 的属性
显示:无
- 请prerendering期间临时asp的控件的Visible属性为true。这从另一个论坛上面链接线程详细说明。
请注意,可见也应在其他SO线程提到的是这个FileUpload控件的父母如此。
Please note that the Visible should also be true for parents of this FileUpload control as mentioned in the other SO thread.
这篇关于自动上传文件到服务器的更新面板内不工作的第一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!