我知道有很多关于同一主题的问题,但我还没有看到能解决我的问题的问题。所以我的 ContentTemplate
中有一个 UpdatePanel
我有一个带有 Panel
的 ListView
,里面有一个 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/