文件上传内容为空

FileUpload content is empty

我知道有很多关于同一主题的问题,但我没有看到解决我的问题的问题。所以我有一个 UpdatePanel 在它的 ContentTemplate 我有一个 ListView 和一个 Panel,里面有一个 FileUpload,一个取消按钮和一个上传按钮.当按下上传按钮时,它会在服务器端调用一个方法来处理文件上传业务。我的问题是 HttpFileCollection 对象是空的,即使我确实选择了一些东西。

这是我正在做的一个例子。按照公司的政策,我不能 post 原始程序,但这应该足够了,因为它是上传时唯一的东西 运行。

客户端:

<asp:UpdatePanel ID="upMain" runat="server">
<ContentTemplate>
    <asp:ListView>
        <asp:Panel ID="pnlFileUpload" runat="server" CssClass ="custom-menu">
            <asp:FileUpload ID="fuUpload" runat="server" AllowMultiple="true" />
            <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="OnUploadFile" />
            <asp:Button ID="btnCancel" runat="server" Text="Cancel" />                                    
        </asp:Panel>

        <asp:ModalPopupExtender ID="mpeUpload" runat="server" TargetControlID ="imgBtnUploadFile" PopupControlID="pnlFileUpload" CancelControlID="btnCancel"
    BackgroundCssClass="modalBackgroud">
            <Animations>
            <OnShown>
                <%--The FadeIn and Display animation.--%>
                <FadeIn Duration="0.25" MinimumOpacity="0" MaximumOpacity="1" />
            </OnShown>
            </Animations>
        </asp:ModalPopupExtender>
    </asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>

服务器端:

protected void OnUploadFile(object sender, EventArgs e)
{
    HttpFileCollection files = Request.Files; 
}

我在 files 上有一个断点,当它命中时,keys 为 0,content 为空,InputStream 也为空。我尝试了一些不同的方法,从设置页面的 enctype ="multipart/form-data" 到许多其他我现在不记得的事情。

仍在掌握 asp 的窍门,因此,如果有任何带有 解释 的建议,我们将不胜感激。

您的错误很可能是由于您的 UpdatePanel。控制的前提很精彩,可悲的是执行非常糟糕。它通常会导致您的 页面状态 出现问题。它在 UpdatePanel 的实际工作方式中做到了这一点。

控件将复制您的页面,将其存储在内存中,然后重新加载整个页面。这会影响您的性能,但副作用是当页面重新加载时,它将经历 Asp.Net 页面生命周期 。如果您不小心,可能会造成一系列问题。

我会做的是将您的 FileUploadUpload Button 移出面板,验证它是否正确上传。一旦确认,您就知道 UpdatePanel 是罪魁祸首。然后你可以在限制范围内工作,否则我会手动 Ajax 请求。

希望这个解释对你来说更详细。

来自MSDN

The following ASP.NET controls are not compatible with partial-page updates, and are therefore not designed to work inside an UpdatePanel control:

...

FileUpload and HtmlInputFile controls when they are used to upload files as part of an asynchronous postback.
...

To use a FileUpload or HtmlInputFile control inside an UpdatePanel control, set the postback control that submits the file to be a PostBackTrigger control for the panel. The FileUpload and HtmlInputFile control can be used only in postback scenarios.