我的文件上传控件不是UpdatePanel的一部分。它只是位于asp:panel
。当我单步执行调试器时,我看到FileName属性已正确填充,FileBytes,PostedFile等,但仅HasFile为false。我最终试图解决这个问题,因为FileName属性具有期望值但HasFile为false。这solution也无济于事!
<asp:Panel id="AddEditReport" runat="server" Visible="false" style= "display:inline;" >
<asp:HiddenField ID="ReportField" runat="server" Value="Report" />
<table width="100%">
<tr>
<td>Title:</td>
<td><asp:TextBox ID="TextBox_Report" runat="server" Width="342px"></asp:TextBox></td>
</tr>
<tr>
<td valign="top"><asp:Label ID="Description_Label" runat="server" Text="Header Description" /></td>
<td><asp:TextBox TextMode="MultiLine" ID="Description" runat="server" Width="342px" height="250px" AutoPostBack="false"/></td>
</tr>
<tr>
<td>
<asp:Label ID="DisplayOrder_Label" runat="server" Text="Display Order" />
</td>
<td>
<asp:TextBox ID="TextBox_DisplayOrder" runat="server" Width="42px" MaxLength="2" AutoPostBack="false"/>
</td>
</tr>
<tr>
<td><asp:Label runat="server" ID="FileUploader_Label" Text="Copy of file: "/></td>
<td><asp:FileUpload ID="FileUpload_Report" runat="server"/></td>
</tr>
</table></asp:Panel>
<asp:Panel ID="Button" runat="server">
<asp:Button id="Save" runat="server" Text="Save" OnClick="SaveButton_clicked"/>
<asp:button id="Back" runat="server" Text="Back" OnClick="BackButton_clicked" UseSubmitBehavior="False"></asp:button><%
int id;
if (!string.IsNullOrEmpty(Request.QueryString[QuerystringID]))
{
id = int.Parse(Request.QueryString[QuerystringID]);
if (string.IsNullOrEmpty(ReportRepository.GetReportById(id).FilePath))
{
%> <a onclick="return confirm('Are you sure you wish to delete this heading?')" href="FileUpload.aspx?id=<%=id.ToString()%>&delete=true" target="_self">
<input type="button" value="Delete Heading"/>
</a>
<%
}
}
%></asp:Panel>
这是我的整个页面。
HasFile失败的代码段后面的代码:
if (FileUpload_Report.HasFile)/*Fails always*/
{
FileInfo fileInfo = new FileInfo(FileUpload_Report.FileName);
// First add the report to get the ID.
ReportRepository.Add(report);
// Then update with the created filepath.
report.FilePath = PathRoot + @"/" + GetFilename(report) + fileInfo.Extension;
// Update the old report with a deleteddate.
ReportRepository.Update(oldReport);
ReportRepository.Update(report);
FileUpload_Report.SaveAs(Server.MapPath(PathRoot) + "\\" + GetFilename(report) + fileInfo.Extension);
}
请帮忙。
答案 0 :(得分:1)
我发现如果文件为空[0 KB],那么它也将返回false。文件中必须有一些东西才能让.HasFile承认它。
答案 1 :(得分:0)
尝试以下操作,让我知道它是否有效:
if (FileUpload_Report.PostedFile == null || string.IsNullOrEmpty(FileUpload_Report.PostedFile.FileName) || FileUpload_Report.PostedFile.InputStream == null) {
// File Doesn't Exist
} Else {
// File Exist
}
答案 2 :(得分:0)
确保在执行SaveButton_clicked之前可以看到AddEditReport面板。我只是这么说,因为我注意到你从Visible =&#34; false&#34;开始。谁知道你的代码还在做什么。
如果出现类似的情况,上传控件将失去其状态