简体   繁体   English

UpdatePanel中的ASP.NET FileUpload - 仍然无法正常工作

[英]ASP.NET FileUpload in UpdatePanel - still not working

Attempting to use a FileUpload or AsyncFileUpload control in an updatepanel on a NET 4.5/C# web application. 尝试在NET 4.5 / C#Web应用程序的updatepanel中使用FileUpload或AsyncFileUpload控件。

I've tried using either standard Scriptmanager or ToolKitScriptManager in my masterpage. 我已尝试在我的母版页中使用标准Scriptmanager或ToolKitScriptManager。

My Save button is set as a PostBackTrigger (tried AsyncPostbackTrigger too). 我的保存按钮被设置为PostBackTrigger(也尝试过AsyncPostbackTrigger)。

No matter what, my (Async)FileUpload.HasFile always returns false. 无论如何,我的(异步)FileUpload.HasFile总是返回false。

Remove the updatepanel and both uploadcontrols work fine. 删除updatepanel,两个uploadcontrols工作正常。

What really throws me is that I have this working in another project (scriptmanager in masterpage, Fileupload in updatepanel, SaveButton is PostbackTrigger). 真正让我感到震惊的是我在另一个项目中工作(masterpage中的scriptmanager,updatepanel中的Fileupload,SaveButton是PostbackTrigger)。

Is there some specific AJAX version or .NET version that can cause problems? 是否有某些特定的AJAX版本或.NET版本会导致问题?

This is extremely frustrating. 这非常令人沮丧。

Adding the button to the UpdatePanel's trigger tag, I got it working: 将按钮添加到UpdatePanel的触发器标签,我得到了它的工作:

<asp:UpdatePanel ID="UpdatePanel" runat="server">
    <ContentTemplate>
        <asp:FileUpload ID="FileUpload" runat="server" />
        <asp:Button ID="btnUpload" runat="server" Text="Upload"
           OnClick = "btnUpLoad_OnClick" />               
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID = "btnUpload" />
    </Triggers>
</asp:UpdatePanel>

I did not have to do anything different server-side (like user5159158's answer). 我没有必要做任何不同的服务器端(如user5159158的答案)。

File Upload will not work with a partial post back. 文件上传不适用于部分回发。 It requires full page request. 它需要完整的页面请求。 So add the below line in your page load. 因此,在页面加载中添加以下行。

ScriptManager.GetCurrent(this).RegisterPostBackControl(this.YourControlID);

在Page_Load中添加: Page.Form.Attributes.Add("enctype", "multipart/form-data");

FileUpload 上传文件

FileUpload requires full page request. FileUpload需要完整的页面请求。 This is a limitation of the XmlHttpRequest component used in all AJAX frameworks for asynchronous calls to the application. 这是对所有AJAX框架中使用的XmlHttpRequest组件的限制,用于对应用程序的异步调用。

What really throws me is that I have this working in another project (scriptmanager in masterpage, Fileupload in updatepanel, SaveButton is PostbackTrigger). 真正让我感到震惊的是我在另一个项目中工作(masterpage中的scriptmanager,updatepanel中的Fileupload,SaveButton是PostbackTrigger)。

I think you are using Full PostBack, although FileUpload is inside **UpdatePanel . 我认为你正在使用Full PostBack,虽然FileUpload位于** UpdatePanel内

For example, 例如,

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
   <ContentTemplate>
      <asp:FileUpload ID="FileUpload1" runat="server" />
      <asp:Button ID="SaveButton" runat="server" OnClick="SaveButton_Click" 
          Text="Upload your file" />
   </ContentTemplate>
   <Triggers>
      <asp:PostBackTrigger ControlID="SaveButton" />
   </Triggers>
</asp:UpdatePanel>

AsyncFileUpload AsyncFileUpload

If you use AsyncFileUpload with UpdatePanel , AsyncFileUpload.HasFile should only checked inside UploadedComplete (you cannot check inside Button click event) . 如果将AsyncFileUploadUpdatePanel 一起使用,则AsyncFileUpload.HasFile应仅在UploadedComplete中检查(您无法检查Button click事件内部)

The reason is AsyncFileUpload is uploaded the file via Async by itself. 原因是AsyncFileUpload通过Async自身上传文件。

Note: make sure you use ToolkitScriptManager instead of ScriptManager 注意: 确保使用ToolkitScriptManager而不是ScriptManager

<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="Server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <ajaxToolkit:AsyncFileUpload runat="server" ID="AsyncFileUpload1"
            OnUploadedComplete="AsyncFileUpload1_UploadedComplete" />
        <asp:TextBox runat="server" ID="TextBox1" /><br/>
        <asp:Button ID="SaveButton" runat="server" OnClick="SaveButton_Click"
            Text="Save" />
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="SaveButton" />
    </Triggers>
</asp:UpdatePanel>

private string FileName
{
    get { return (string)(Session["FileName"] ?? ""); }
    set { Session["FileName"] = value; }
}

protected void SaveButton_Click(object sender, EventArgs e)
{
    string fileName = FileName;
    string path = Server.MapPath("~/App_Data/");
    var fileInfo = new FileInfo(path + FileName);
}

protected void AsyncFileUpload1_UploadedComplete(object sender, 
    AsyncFileUploadEventArgs e)
{
    if (AsyncFileUpload1.HasFile)
    {
        FileName = AsyncFileUpload1.FileName;
        string path = Server.MapPath("~/App_Data/");
        AsyncFileUpload1.SaveAs(path + AsyncFileUpload1.FileName);
    }
}

Other Thoughts 其他想法

I personally do not like using AsyncFileUpload inside UpdatePanel . 我个人不喜欢在UpdatePanel中使用AsyncFileUpload Instead, I'll rather use Full PostBack if I need to upload a file. 相反,如果我需要上传文件,我宁愿使用Full PostBack。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM