c# - 文件上传内容为空

标签 c# asp.net file-upload

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

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

客户端:

<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 为空,输入流。我尝试了一些不同的操作,从设置页面的 enctype ="multipart/form-data" 到我现在不记得的许多其他操作。

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

最佳答案

来自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.

关于c# - 文件上传内容为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30400161/

相关文章:

c# - 如何检查任何 ContentDialog 是否打开?

c# - 用 C# 绘图

c# - 使用 LINQ to SQL 在数据库之间复制

asp.net - Node Js - Mongo 动态查询字符串数组

c# - Gridview EditItemTemplate DropDownList 获取 SelectedValue

c# - 当客户在加拿大时,如何让 Stripe 结账显示 249 加元且含 5% 税的价格,而在世界其他地区则显示 249 美元且不含税?

c# - asp.net c# 中的字 rune 字过多

javascript - 是否可以执行异步跨域文件上传?

java - 使用 FileUpload 时嵌套表单上的 Wicket 口问题

php - Laravel 5 文件上传验证