【问题标题】:FileUpload control HasFile fails but file is actually availableFileUpload 控件 HasFile 失败但文件实际可用
【发布时间】:2014-03-28 16:24:01
【问题描述】:

我的文件上传控件不是 UpdatePanel 的一部分。它只是驻留在asp:panel 中。当我单步调试调试器时,我看到正确填充了 FileName 属性、FileBytes、PostedFile 等,但 HasFile 本身是错误的。由于 FileName 属性具有预期值,但 HasFile 为假,因此我束手无策。这个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 失败的 sn-p 背后的代码:

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);
}

请帮忙。

【问题讨论】:

  • 为什么不把 FileUpload 放到更新面板中?

标签: c# asp.net file-upload


【解决方案1】:

我发现如果文件为空 [0 KB],那么它也会返回 false。文件中必须有某些内容才能让 .HasFile 确认它。

【讨论】:

    【解决方案2】:

    尝试以下方法,如果有效,请告诉我:

    if (FileUpload_Report.PostedFile == null || string.IsNullOrEmpty(FileUpload_Report.PostedFile.FileName) || FileUpload_Report.PostedFile.InputStream == null) { 
       // File Doesn't Exist
    } Else {
       // File Exist
    }
    

    【讨论】:

      【解决方案3】:

      确保在执行 SaveButton_clicked 之前 AddEditReport 面板可见。我之所以这么说,是因为我注意到您从 Visible="false" 开始,谁知道您的代码还在做什么。

      如果发生这种情况,上传控件将失去其状态

      1. 某处的事件设置 AddEditReport.Visible = true
      2. 用户使用文件上传控件
      3. 某处的事件设置 AddEditReport.Visible = false
      4. 用户点击保存按钮
      5. 上传控制失去其状态

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-03
        • 1970-01-01
        • 2022-01-11
        • 1970-01-01
        • 2010-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多