上传失败一次,然后就可以正常了

发布于 2024-08-10 07:04:36 字数 1186 浏览 1 评论 0原文

我正在开发 ASP.Net 自定义控件。在我的控件中,我有一个 FileUpload 控件,位于 MultiView 内,位于 AJAX UpdatePanel 内。

我已将提交按钮添加到更新面板的回发触发器中。 (这是让 FileUpload 在 UpdatePanel 中工作的标准修复)。

第一次提交时,FileUpload 不会上传任何内容(即控件的 FileBytes 属性的长度为零)。表单上的其他所有内容均已正确提交。

在第二次及后续提交时,上传工作正常。

什么可能导致此问题?如何解决?


例如:

<asp:UpdatePanel runat="server" ID="update_panel" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:MultiView runat="server" ID="mvMultiView" ActiveViewIndex="0">
            <asp:View runat="server" ID="viewOne">
                <!-- content -->
            </asp:View>
            <asp:View runat="server" ID="viewTwo">
                <!-- some other form elements -->
                <asp:FileUpload ID="file_upload" runat="server" />
                <asp:Button ID="save_button" runat="server" Text="Save" OnClick="save_Click" ValidationGroup="group" />
            </asp:View>
         </asp:MultiView>
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="save_button" />
    </Triggers>
</ajax:UpdatePanel>

I'm working on an ASP.Net custom control. In my control I have a FileUpload control, inside a MultiView, inside an AJAX UpdatePanel.

I've added the submit button to the post back triggers of the update panel. (This is the standard fix for getting a FileUpload to work within an UpdatePanel).

On the first submit the FileUpload does not upload anything (ie, the control's FileBytes property has zero length). Everything else on the form submits properly.

On the second and subsequent submits, the upload works correctly.

What could cause this, and how do I fix it?


For example:

<asp:UpdatePanel runat="server" ID="update_panel" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:MultiView runat="server" ID="mvMultiView" ActiveViewIndex="0">
            <asp:View runat="server" ID="viewOne">
                <!-- content -->
            </asp:View>
            <asp:View runat="server" ID="viewTwo">
                <!-- some other form elements -->
                <asp:FileUpload ID="file_upload" runat="server" />
                <asp:Button ID="save_button" runat="server" Text="Save" OnClick="save_Click" ValidationGroup="group" />
            </asp:View>
         </asp:MultiView>
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="save_button" />
    </Triggers>
</ajax:UpdatePanel>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心头的小情儿 2024-08-17 07:04:37

事实证明,这是由部分页面更新期间呈现的 FileUpload 控件引起的。要正确提交,需要使用完整回发来呈现和提交 FileUpload 控件。由于第一次表单提交导致了完整的回发,因此第二次上传开始按预期进行。

解决方案:
添加两个显示文件上传控件并将其提交到更新面板回发触发器的按钮。

例如:

   <Triggers>
      <asp:PostBackTrigger ControlID="btnShowUploadForm" />
      <asp:PostBackTrigger ControlID="btnUpload" />
   </Triggers>                            

This turned out to be caused by the FileUpload Control being rendered during a partial page update. To submit properly the FileUpload control needs to be rendered and submitted using full post backs. Because the first form submit caused a full post back, the upload began working as intended the second time around.

Solution:
Add both of the buttons that display and submit the file upload control to the update panels post back triggers.

eg:

   <Triggers>
      <asp:PostBackTrigger ControlID="btnShowUploadForm" />
      <asp:PostBackTrigger ControlID="btnUpload" />
   </Triggers>                            
野鹿林 2024-08-17 07:04:37

我尝试在页面加载事件中添加下面的行,它第一次和随后的时间都有效。

Page.Form.Enctype = “多部分/表单数据”;

在源中显式指定表单标记将不起作用。

I tried adding the line below in the Page Load event and it works the first time and subsequent times.

Page.Form.Enctype = “multipart/form-data”;

Explicitly specifying the form tag in source will not work.

黯然 2024-08-17 07:04:37

我的代码几乎与您的代码相同,工作正常,除了我的 UpdateMode=Conditional:

  <asp:UpdatePanel ID="upUpload" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
    <asp:RadioButton ID="rbImage" GroupName="resourceupload" Checked="true" Text="image" runat="server" />
    <asp:RadioButton ID="rbPdf" GroupName="resourceupload" Text="pdf" runat="server" />
     <asp:FileUpload ID="FileUpload1" runat="server"  Width="175"/>
    <asp:Button ID="btnUpload" runat="server" CausesValidation="false"
    Text="Upload" OnClick="btnUpload_Click" />
    <asp:Label ID="lblMsg" Visible="false" runat="server" Text=""></asp:Label>

    </ContentTemplate> 
    <Triggers>
      <asp:PostBackTrigger ControlID="btnUpload" />
      </Triggers>                            
   </asp:UpdatePanel>

I have code almost identical to yours that works fine, except my UpdateMode=Conditional:

  <asp:UpdatePanel ID="upUpload" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
    <asp:RadioButton ID="rbImage" GroupName="resourceupload" Checked="true" Text="image" runat="server" />
    <asp:RadioButton ID="rbPdf" GroupName="resourceupload" Text="pdf" runat="server" />
     <asp:FileUpload ID="FileUpload1" runat="server"  Width="175"/>
    <asp:Button ID="btnUpload" runat="server" CausesValidation="false"
    Text="Upload" OnClick="btnUpload_Click" />
    <asp:Label ID="lblMsg" Visible="false" runat="server" Text=""></asp:Label>

    </ContentTemplate> 
    <Triggers>
      <asp:PostBackTrigger ControlID="btnUpload" />
      </Triggers>                            
   </asp:UpdatePanel>
乖乖 2024-08-17 07:04:37

FileUpload 控件是 UpdatePanel 不支持的控件之一。

不过还有其他几个库可以进行 AJAX 上传。 Telerik 有一个不错的,我以前用过,但它不是免费的。虽然有一些不错的免费软件,但您必须尝试一些。每种都有其优点和缺点。

The FileUpload control is one of the controls not supported by UpdatePanel.

There are several other libraries out there for doing AJAX uploads though. Telerik has a nice one I've used before, but it's not free. There are some decent free ones though, but you'll have to try a few out. Each has it's advantages and disadvantages.

偏闹i 2024-08-17 07:04:37

同时添加 btnShowUploadForm 的解决方案将否定更新面板的目的。您还可以从更新面板中删除数据访问控制(文件上传控件所在的位置)。更新面板的重点是 btnShowUploadForm 和取消按钮(取消上传,如果有的话),至少引起异步回发以获得更好的用户体验。

The solution to also add the btnShowUploadForm will negate the purpose of the update panel. You might as well remove the data access control (where the fileupload control is residing), out of the update panel. The point of the update panel is for the btnShowUploadForm and the Cancel button (to cancel upload, if any), to at least cause a asynchpostback for better user experience.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文