本文介绍了ASP.NET文件上传在UpdatePanel的 - 仍然没有工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图在一个UpdatePanel上NET 4.5 / C#的Web应用程序使用一个FileUpload或AsyncFileUpload控制。

Attempting to use a FileUpload or AsyncFileUpload control in an updatepanel on a NET 4.5/C# web application.

我在我的母版使用标准的ScriptManager或ToolKitScriptManager尝试。

I've tried using either standard Scriptmanager or ToolKitScriptManager in my masterpage.

我的保存按钮设置为PostBackTrigger(试过AsyncPostbackTrigger太)。

My Save button is set as a PostBackTrigger (tried AsyncPostbackTrigger too).

不管是什么,我的(异步)FileUpload.HasFile始终返回false。

No matter what, my (Async)FileUpload.HasFile always returns false.

删除的UpdatePanel无一不uploadcontrols做工精细。

Remove the updatepanel and both uploadcontrols work fine.

真正引发我的是,我有这个工作在另一个项目(母版中的ScriptManager,文件上传在UpdatePanel中,SaveButton是PostbackTrigger)。

What really throws me is that I have this working in another project (scriptmanager in masterpage, Fileupload in updatepanel, SaveButton is PostbackTrigger).

有没有一些具体的AJAX版本或.NET版本可能会导致问题?

Is there some specific AJAX version or .NET version that can cause problems?

这是非常令人沮丧。

推荐答案

文件上传需要整页的请求。这是在所有的AJAX框架异步调用的应用程序使用的XmlHtt prequest组分的限制。

FileUpload

FileUpload requires full page request. This is a limitation of the XmlHttpRequest component used in all AJAX frameworks for asynchronous calls to the application.

真正引发我的是,我有这个工作在另一个项目
  (在ScriptManager的母版,在文件上传的UpdatePanel,SaveButton是
  PostbackTrigger)。

我想你使用的是完全回发,虽然的FileUpload里面**的UpdatePanel

I think you are using Full PostBack, although FileUpload is inside **UpdatePanel.

例如,

<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 的UpdatePanel AsyncFileUpload.HasFile 应只检查里面的 UploadedComplete (你不能按钮点击事件里面检查)

AsyncFileUpload

If you use AsyncFileUpload with UpdatePanel, AsyncFileUpload.HasFile should only checked inside UploadedComplete (you cannot check inside Button click event).

原因是的 AsyncFileUpload 上传通过文件的异步本身。

The reason is AsyncFileUpload is uploaded the file via Async by itself.

注:确保您使用ToolkitScriptManager而不是ScriptManager的

Note: make sure you use ToolkitScriptManager instead of 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);
    }
}

其他的想法

我个人不喜欢使用的 AsyncFileUpload 的UpdatePanel 。相反,我宁愿用完全回发,如果我需要上传一个文件。

Other Thoughts

I personally do not like using AsyncFileUpload inside UpdatePanel. Instead, I'll rather use Full PostBack if I need to upload a file.

这篇关于ASP.NET文件上传在UpdatePanel的 - 仍然没有工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 20:34